Skip to content

Instantly share code, notes, and snippets.

@wise-io
Last active July 12, 2022 16:12
Show Gist options
  • Save wise-io/507839c9f96ae690fbcc248d84480c46 to your computer and use it in GitHub Desktop.
Save wise-io/507839c9f96ae690fbcc248d84480c46 to your computer and use it in GitHub Desktop.
Installs PWSH (PowerShell 7) and restarts script in PWSH
# 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