3434 # Check if PowerShell is already installed and compare versions
3535 $needToInstall = $true
3636 try {
37- $currentInstall = Get-Package -Name "PowerShell*" -ErrorAction SilentlyContinue | Select-Object -First 1
37+ $currentInstall = Get-Command pwsh -ErrorAction SilentlyContinue
3838 if ($currentInstall) {
39- $currentVersion = $currentInstall.Version
39+ $currentVersion = & pwsh -Command '$PSVersionTable.PSVersion.ToString()'
4040 Write-Host "Current PowerShell version: $currentVersion"
4141
4242 # Parse versions into System.Version objects for proper comparison
@@ -45,45 +45,47 @@ runs:
4545 $targetVersionObj = [System.Version]$version
4646
4747 # Compare versions
48- $comparison = $currentVersionObj.CompareTo($targetVersionObj)
49- if ($comparison -ge 0) {
48+ if ($currentVersionObj -ge $targetVersionObj) {
5049 Write-Host "Current version $currentVersion is greater than or equal to target version $version. Skipping installation."
5150 $needToInstall = $false
5251 } else {
5352 Write-Host "Current version $currentVersion is lower than target version $version. Will upgrade."
54-
55- # Uninstall current version
56- Write-Host "Uninstalling $($currentInstall.Name) version $($currentInstall.Version)"
57- Uninstall-Package -Name $currentInstall.Name -Force -ErrorAction SilentlyContinue
5853 }
5954 } catch {
6055 Write-Host "Error comparing versions: $_ Will proceed with installation."
56+ $needToInstall = $true
6157 }
6258 } else {
6359 Write-Host "PowerShell Core not found. Will install version $version"
6460 }
6561 } catch {
6662 Write-Host "Error checking current PowerShell version: $_"
63+ $needToInstall = $true
6764 }
6865
6966 if ($needToInstall) {
70- # Download and install PowerShell
71- $msiName = "PowerShell-$version-win-x64.msi"
72- $downloadUrl = "https://github.com/PowerShell/PowerShell/releases/download/v$version/$msiName"
73-
74- Write-Host "Downloading from $downloadUrl"
75- Invoke-WebRequest -Uri $downloadUrl -OutFile $msiName
76-
77- Write-Host "Installing PowerShell $version"
78- Start-Process -FilePath "msiexec.exe" -ArgumentList "/i", $msiName, "/quiet", "/norestart" -Wait
79-
80- # Verify installation
81- $pwshPath = "$env:ProgramFiles\PowerShell\7\pwsh.exe"
82- if (Test-Path $pwshPath) {
83- Write-Host "PowerShell installed successfully:"
84- & $pwshPath -Command '$PSVersionTable'
85- } else {
86- Write-Error "PowerShell installation failed. Could not find $pwshPath"
67+ try {
68+ # Download and install PowerShell
69+ $msiName = "PowerShell-$version-win-x64.msi"
70+ $downloadUrl = "https://github.com/PowerShell/PowerShell/releases/download/v$version/$msiName"
71+
72+ Write-Host "Downloading from $downloadUrl"
73+ Invoke-WebRequest -Uri $downloadUrl -OutFile $msiName -ErrorAction Stop
74+
75+ Write-Host "Installing PowerShell $version"
76+ Start-Process -FilePath "msiexec.exe" -ArgumentList "/i", $msiName, "/quiet", "/norestart" -Wait -ErrorAction Stop
77+
78+ # Verify installation
79+ $pwshPath = "$env:ProgramFiles\PowerShell\7\pwsh.exe"
80+ if (Test-Path $pwshPath) {
81+ Write-Host "PowerShell installed successfully:"
82+ & $pwshPath -Command '$PSVersionTable'
83+ } else {
84+ Write-Error "PowerShell installation failed. Could not find $pwshPath"
85+ exit 1
86+ }
87+ } catch {
88+ Write-Error "Failed to install PowerShell: $_"
8789 exit 1
8890 }
8991 }
0 commit comments