Public/Enable-WingetUpdateNotifications.ps1
|
function Enable-WingetUpdateNotifications { <# .SYNOPSIS Enable automatic winget update notifications in your PowerShell profile. .DESCRIPTION Adds a background check to your PowerShell profile that monitors for winget package updates. The check runs when you open a terminal and can optionally run on an interval. .PARAMETER Interval How often to check for updates (in hours). Default is 3 hours. Set to 0 to only check when opening a new terminal. .PARAMETER CheckOnStartup Check for updates every time you open a terminal. Default is $true. .EXAMPLE Enable-WingetUpdateNotifications Enables update notifications with default settings (check on startup and every 3 hours). .EXAMPLE Enable-WingetUpdateNotifications -Interval 6 Check every 6 hours instead of 3. .EXAMPLE Enable-WingetUpdateNotifications -Interval 0 -CheckOnStartup $true Only check when opening a terminal, not on an interval. #> [CmdletBinding()] param( [Parameter()] [int]$Interval = 3, [Parameter()] [bool]$CheckOnStartup = $true ) $configDir = Get-WingetBatchConfigDir $configFile = Join-Path $configDir "config.json" # Create config directory if it doesn't exist if (-not (Test-Path $configDir)) { New-Item -ItemType Directory -Path $configDir -Force | Out-Null } # Save configuration $config = @{ UpdateNotificationsEnabled = $true CheckInterval = $Interval CheckOnStartup = $CheckOnStartup LastCheck = $null } $config | ConvertTo-Json | Out-File -FilePath $configFile -Encoding UTF8 -Force # Add to PowerShell profile $profilePath = $PROFILE.CurrentUserAllHosts if (-not (Test-Path $profilePath)) { New-Item -ItemType File -Path $profilePath -Force | Out-Null } $profileContent = Get-Content $profilePath -Raw -ErrorAction SilentlyContinue $initCode = @' # WingetBatch - Update Notifications if (Get-Module -ListAvailable -Name WingetBatch) { Import-Module WingetBatch -ErrorAction SilentlyContinue Start-WingetUpdateCheck } '@ if ($profileContent -notmatch 'Start-WingetUpdateCheck') { Add-Content -Path $profilePath -Value $initCode Write-Host "✓ Update notifications enabled!" -ForegroundColor Green Write-Host " Configuration saved to: $configFile" -ForegroundColor DarkGray Write-Host " Profile updated: $profilePath" -ForegroundColor DarkGray Write-Host "" Write-Host "Restart your terminal or run: " -NoNewline -ForegroundColor Cyan Write-Host ". `$PROFILE" -ForegroundColor Yellow } else { Write-Host "✓ Configuration updated!" -ForegroundColor Green Write-Host " Update notifications were already enabled in your profile." -ForegroundColor DarkGray } } |