11name : Install PowerShell
2- description : Install a specific version of PowerShell Core on any GitHub runner (Linux, macOS, Windows). Handles install, downgrade, or forced reinstall .
2+ description : Install a specific version of PowerShell Core on any GitHub runner (Linux, macOS, Windows). Installs only when the requested version is not already present .
33author : PSModule
44branding :
55 icon : terminal
99 Version :
1010 description : Exact PowerShell version to install (e.g. 7.4.1)
1111 required : true
12- Reinstall :
13- description : Set to 'true' to uninstall & reinstall even when the exact version is already present.
14- default : ' false'
1512
1613runs :
1714 using : composite
@@ -20,161 +17,81 @@ runs:
2017 if : runner.os == 'Linux'
2118 env :
2219 REQUESTED_VERSION : ${{ inputs.Version }}
23- REINSTALL : ${{ inputs.Reinstall }}
2420 shell : bash
2521 run : |
2622 set -e
27-
28- echo "Detecting existing PowerShell on Linux..."
2923 DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
30- echo "Detected version: ${DETECTED_VERSION:-<none>}"
31- echo "Requested version: $REQUESTED_VERSION"
32- echo "Reinstall flag: $REINSTALL"
33-
34- if [[ -z "$DETECTED_VERSION" ]]; then
35- DECISION="install"
36- elif [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
37- if [[ "$REINSTALL" == "true" ]]; then DECISION="reinstall"; else DECISION="skip"; fi
38- else
39- DECISION="uninstall-install" # covers downgrade or upgrade
40- fi
41- echo "Decision: $DECISION"
42-
43- if [[ "$DECISION" == "skip" ]]; then
44- echo "Skipping installation on Linux (exact version present)."
24+ if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
25+ echo "PowerShell $DETECTED_VERSION already installed. Skipping."
4526 exit 0
4627 fi
4728
48- # -------------------------------------------------------------------------------------------------
49- # Debian/Ubuntu installation
50- # -------------------------------------------------------------------------------------------------
5129 if command -v apt-get >/dev/null; then
5230 echo "Using APT package manager (Debian/Ubuntu)..."
5331
54- echo "Ensuring Microsoft package repo is configured..."
5532 if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
5633 wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
5734 DIST_CODENAME=$(lsb_release -cs)
5835 sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
5936 fi
6037
61- echo "Updating package index..."
6238 sudo apt-get update -y
63-
64- if [[ "$DECISION" != "install" ]]; then
65- echo "Removing any existing PowerShell packages..."
66- sudo apt-get remove -y powershell || true
67- sudo apt-get autoremove -y || true
68- fi
69-
70- echo "Attempting to install exact version $REQUESTED_VERSION from repo..."
7139 EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' | grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)
7240 if [[ -n "$EXACT_PKG" ]]; then
7341 sudo apt-get install -y powershell=$EXACT_PKG
7442 else
75- echo "Exact version not found in repo; downloading .deb package from GitHub release..."
7643 ARCH=$(dpkg --print-architecture)
7744 DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
7845 URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
79- echo "Fetching $URL"
8046 wget -q "$URL" -O "$DEB_NAME"
8147 sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
8248 fi
83-
8449 else
85- echo "Unsupported Linux distribution (no apt-get). PowerShell install via package manager not implemented. "
50+ echo "Unsupported Linux distribution (no apt-get)."
8651 exit 1
8752 fi
8853
8954 - name : Install PowerShell (macOS)
9055 if : runner.os == 'macOS'
9156 env :
9257 REQUESTED_VERSION : ${{ inputs.Version }}
93- REINSTALL : ${{ inputs.Reinstall }}
9458 shell : bash
9559 run : |
9660 set -e
97-
98- echo "Detecting existing PowerShell on macOS..."
9961 DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
100- echo "Detected version: ${DETECTED_VERSION:-<none>}"
101- echo "Requested version: $REQUESTED_VERSION"
102- echo "Reinstall flag: $REINSTALL"
103-
104- if [[ -z "$DETECTED_VERSION" ]]; then
105- DECISION="install"
106- elif [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
107- if [[ "$REINSTALL" == "true" ]]; then DECISION="reinstall"; else DECISION="skip"; fi
108- else
109- DECISION="uninstall-install"
62+ if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
63+ echo "PowerShell $DETECTED_VERSION already installed. Skipping."
64+ exit 0
11065 fi
111- echo "Decision: $DECISION"
11266
113- if [[ "$DECISION" == "skip" ]] ; then
114- echo "Skipping installation on macOS (exact version present)."
67+ if brew info --cask powershell@$REQUESTED_VERSION >/dev/null 2>&1 ; then
68+ brew install --cask powershell@$REQUESTED_VERSION
11569 else
116- echo "Uninstalling any existing PowerShell..."
117- brew uninstall --ignore-dependencies --force powershell || true
118-
119- echo "Attempting to install PowerShell $REQUESTED_VERSION via Homebrew..."
120- if brew info --cask powershell@$REQUESTED_VERSION >/dev/null 2>&1; then
121- brew install --cask powershell@$REQUESTED_VERSION
122- else
123- echo "Exact version not available via Homebrew; downloading .pkg from GitHub release..."
124- ARCH=$(uname -m)
125- if [[ "$ARCH" == "arm64" ]]; then PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg"; else PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg"; fi
126- URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
127- echo "Fetching $URL"
128- curl -sSL "$URL" -o "$PKG_NAME"
129- sudo installer -pkg "$PKG_NAME" -target /
130- fi
70+ ARCH=$(uname -m)
71+ if [[ "$ARCH" == "arm64" ]]; then PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg"; else PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg"; fi
72+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
73+ curl -sSL "$URL" -o "$PKG_NAME"
74+ sudo installer -pkg "$PKG_NAME" -target /
13175 fi
13276
13377 - name : Install PowerShell (Windows)
13478 if : runner.os == 'Windows'
13579 env :
13680 REQUESTED_VERSION : ${{ inputs.Version }}
137- REINSTALL : ${{ inputs.Reinstall }}
13881 shell : powershell
13982 run : |
140- Write-Host "Detecting existing PowerShell on Windows..."
14183 try {
14284 $detected = (& pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')
14385 } catch {
14486 $detected = $null
14587 }
14688
147- $detectedDisplay = if ([string]::IsNullOrWhiteSpace($detected)) { '<none>' } else { $detected }
148- Write-Host "Detected version: $detectedDisplay"
149- Write-Host "Requested version: $env:REQUESTED_VERSION"
150- Write-Host "Reinstall flag: $env:REINSTALL"
151-
152- if (-not $detected) {
153- $decision = 'install'
154- } elseif ($detected -eq $env:REQUESTED_VERSION) {
155- if ($env:REINSTALL -eq 'true') {
156- $decision = 'reinstall'
157- } else {
158- $decision = 'skip'
159- }
160- } else {
161- $decision = 'uninstall-install'
89+ if ($detected -eq $env:REQUESTED_VERSION) {
90+ Write-Host "PowerShell $detected already installed. Skipping."
91+ exit 0
16292 }
163- Write-Host "Decision: $decision"
16493
165- if ($decision -eq 'skip') {
166- Write-Host 'Skipping installation on Windows (exact version present).'
167- } else {
168- Write-Host 'Removing existing PowerShell installation (if any)...'
169- $pwshProducts = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like 'PowerShell*' }
170- foreach ($p in $pwshProducts) {
171- Write-Host "Uninstalling $($p.Name)..."
172- Start-Process msiexec.exe -ArgumentList '/x', $p.IdentifyingNumber, '/quiet', '/norestart' -Wait
173- }
174-
175- Write-Host "Installing PowerShell $env:REQUESTED_VERSION..."
176- $msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
177- $url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
178- Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
179- Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait
180- }
94+ $msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
95+ $url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
96+ Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
97+ Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait
0 commit comments