Skip to content

Instantly share code, notes, and snippets.

@atrenton
Created December 31, 2022 17:34
Show Gist options
  • Save atrenton/72d6284b29c87b83aeaa3b452bcf79a5 to your computer and use it in GitHub Desktop.
Save atrenton/72d6284b29c87b83aeaa3b452bcf79a5 to your computer and use it in GitHub Desktop.
Upgrade PowerShell 7-x86

The following script will automatically download and install the latest x86 version of PowerShell 7.

# Upgrade-PowerShell.ps1
# REF: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/resolving-urls-3
#requires -version 7.0

$Params = @{
    Method = 'HEAD'
    Uri = 'https://github.com/PowerShell/PowerShell/releases/latest'
    MaximumRedirection = 0
    ErrorAction  = 'SilentlyContinue'
    SkipHttpErrorCheck = $true
}
$latestReleaseUri = (Invoke-WebRequest @Params).Headers.Location

$version = (Split-Path $latestReleaseUri -Leaf).Substring(1)

$platform = 'x86'
$packageName = "PowerShell-$version-win-$platform.msi"
$downloadUri = "$($latestReleaseUri -replace 'tag','download')/$packageName"

"Downloading latest release of PowerShell from:`r`n$downloadUri"

$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
$null = New-Item -ItemType Directory -Path $tempDir -Force -ErrorAction SilentlyContinue
$packagePath = Join-Path -Path $tempDir -ChildPath $packageName

Invoke-WebRequest -Uri $downloadUri -OutFile $packagePath

#REF: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.2
#REF: https://github.com/PowerShell/PowerShell/blob/master/tools/install-powershell.ps1
#ARG REF: ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ENABLE_PSREMOTING=1
#ARG REF: REGISTER_MANIFEST=1 USE_MU=1 ENABLE_MU=1
$MSIArguments = @('/i', $packagePath, 'REGISTER_MANIFEST=1', 'USE_MU=1', 'ENABLE_MU=1')

Start-Process msiexec.exe -ArgumentList $MSIArguments -Verb 'RunAs' -Wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment