UpdatePowerShellGet_v2.ps1
|
# Update PowerShellGet and PackageManagement - Version 2 # This script removes loaded modules before updating Write-Host "====================================================" -ForegroundColor Cyan Write-Host "PowerShellGet/PackageManagement Update - Version 2" -ForegroundColor Cyan Write-Host "====================================================" -ForegroundColor Cyan Write-Host "" # Check if running as admin $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "ERROR: This script must run with administrator privileges!" -ForegroundColor Red Write-Host "Press any key to exit..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") exit 1 } Write-Host "Current loaded modules:" -ForegroundColor Yellow Get-Module -Name PowerShellGet, PackageManagement | Format-Table -AutoSize Write-Host "" # Try to remove loaded modules Write-Host "Attempting to remove loaded modules..." -ForegroundColor Yellow try { Remove-Module -Name PowerShellGet -Force -ErrorAction SilentlyContinue Remove-Module -Name PackageManagement -Force -ErrorAction SilentlyContinue Write-Host "Modules removed from current session." -ForegroundColor Green } catch { Write-Host "Note: Could not remove some modules (this is normal)" -ForegroundColor Yellow } Write-Host "" # Show available versions Write-Host "Currently installed versions:" -ForegroundColor Yellow Get-Module -Name PowerShellGet, PackageManagement -ListAvailable | Select-Object Name, Version, Path | Format-Table -AutoSize Write-Host "" # Update PackageManagement first (using CurrentUser to avoid system locks) Write-Host "====================================================" -ForegroundColor Cyan Write-Host "Step 1: Updating PackageManagement to CurrentUser scope..." -ForegroundColor Green Write-Host "====================================================" -ForegroundColor Cyan try { Install-Module -Name PackageManagement -Force -AllowClobber -Scope CurrentUser -SkipPublisherCheck -ErrorAction Stop Write-Host "SUCCESS: PackageManagement updated!" -ForegroundColor Green } catch { Write-Host "ERROR updating PackageManagement: $($_.Exception.Message)" -ForegroundColor Red } Write-Host "" # Update PowerShellGet Write-Host "====================================================" -ForegroundColor Cyan Write-Host "Step 2: Updating PowerShellGet to CurrentUser scope..." -ForegroundColor Green Write-Host "====================================================" -ForegroundColor Cyan try { Install-Module -Name PowerShellGet -Force -AllowClobber -Scope CurrentUser -SkipPublisherCheck -ErrorAction Stop Write-Host "SUCCESS: PowerShellGet updated!" -ForegroundColor Green } catch { Write-Host "ERROR updating PowerShellGet: $($_.Exception.Message)" -ForegroundColor Red } Write-Host "" # Show updated versions Write-Host "====================================================" -ForegroundColor Cyan Write-Host "Updated module versions:" -ForegroundColor Yellow Write-Host "====================================================" -ForegroundColor Cyan Get-Module -Name PowerShellGet, PackageManagement -ListAvailable | Select-Object Name, Version, Path | Sort-Object Name, Version -Descending | Format-Table -AutoSize Write-Host "" Write-Host "====================================================" -ForegroundColor Cyan Write-Host "IMPORTANT: Close ALL PowerShell windows and reopen" -ForegroundColor Yellow Write-Host "to use the new versions!" -ForegroundColor Yellow Write-Host "====================================================" -ForegroundColor Cyan Write-Host "" Write-Host "Press any key to exit..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |