Last active
July 12, 2022 16:12
-
-
Save wise-io/507839c9f96ae690fbcc248d84480c46 to your computer and use it in GitHub Desktop.
Installs PWSH (PowerShell 7) and restarts script in PWSH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check for required PowerShell version (7+) | |
if (!($PSVersionTable.PSVersion.Major -ge 7)) { | |
try { | |
# Install PowerShell 7 if missing | |
if (!(Test-Path "$env:SystemDrive\Program Files\PowerShell\7")) { | |
Write-Output 'Installing PowerShell version 7...' | |
Invoke-Expression "& { $(Invoke-RestMethod https://aka.ms/install-powershell.ps1) } -UseMSI -Quiet" | |
} | |
# Refresh PATH | |
$env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User') | |
# Restart script in PowerShell 7 | |
pwsh -File "`"$PSCommandPath`"" @PSBoundParameters | |
} | |
catch { | |
Write-Output 'PowerShell 7 was not installed. Update PowerShell and try again.' | |
throw $Error | |
} | |
finally { exit $LASTEXITCODE } | |
} | |
else { $PSStyle.OutputRendering = 'PlainText' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment