Skip to content

Commit ba2b99a

Browse files
Refactor Windows installation: enhance version detection logic, improve error handling, and streamline uninstallation process for existing PowerShell installations.
1 parent f6ff12f commit ba2b99a

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

action.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,12 @@ runs:
105105
run: |
106106
Write-Host "Detecting existing PowerShell on Windows…"
107107
try {
108-
$detected = (pwsh -NoLogo -NoProfile -Command "$PSVersionTable.PSVersion.ToString()")
108+
$detected = (& pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')
109109
} catch {
110110
$detected = $null
111111
}
112112
113-
if ($null -eq $detected -or $detected -eq '') {
114-
$detectedDisplay = '<none>'
115-
} else {
116-
$detectedDisplay = $detected
117-
}
113+
$detectedDisplay = if ([string]::IsNullOrWhiteSpace($detected)) { '<none>' } else { $detected }
118114
Write-Host "Detected version: $detectedDisplay"
119115
Write-Host "Requested version: $env:REQUESTED_VERSION"
120116
Write-Host "Reinstall flag: $env:REINSTALL"
@@ -135,11 +131,16 @@ runs:
135131
if ($decision -eq 'skip') {
136132
Write-Host 'Skipping installation on Windows (exact version present).'
137133
} else {
138-
Write-Host 'Uninstalling any existing PowerShell…'
139-
winget uninstall --id Microsoft.PowerShell -e --silent | Out-Null
134+
Write-Host 'Removing existing PowerShell installation (if any)…'
135+
$pwshProducts = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like 'PowerShell*' }
136+
foreach ($p in $pwshProducts) {
137+
Write-Host "Uninstalling $($p.Name)…"
138+
Start-Process msiexec.exe -ArgumentList '/x', $p.IdentifyingNumber, '/quiet', '/norestart' -Wait
139+
}
140+
140141
Write-Host "Installing PowerShell $env:REQUESTED_VERSION…"
141142
$msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
142143
$url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
143-
Invoke-WebRequest $url -OutFile $msi
144+
Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
144145
Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait
145146
}

0 commit comments

Comments
 (0)