Skip to content

Instantly share code, notes, and snippets.

@pldmgg
Last active January 25, 2021 07:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pldmgg/8af36d44cebd1d61c77e90f7de2f3013 to your computer and use it in GitHub Desktop.
Save pldmgg/8af36d44cebd1d61c77e90f7de2f3013 to your computer and use it in GitHub Desktop.
Upgrade PowerShell Core On Windows 10 x64
function Upgrade-PowerShellCore {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[switch]$PreRelease,
[Parameter(Mandatory=$False)]
[switch]$Silent
)
$randomFileName = [System.IO.Path]::GetRandomFileName()
$tmpMsiPath = Join-Path ([System.IO.Path]::GetTempPath()) "$randomFileName.msi"
$GitHubUrlBase = 'https://api.github.com/repos/powershell/powershell/releases'
if ($PreRelease) {
$url = $GitHubUrlBase
$response = Invoke-RestMethod -Uri $url
$response = $($response | Where-Object {$_.prerelease -eq $True} | Sort-Object -Descending -Property 'published_at')[0]
}
else {
$url = $GitHUbUrlBase + '/latest'
$response = Invoke-RestMethod -Uri $url
}
$PwshAssetUrl = $response.assets.browser_download_url | Where-Object {$_ -match "win" -and $_ -match "64" -and $_ -match "\.msi$"}
$null = Invoke-RestMethod -Uri $PwshAssetUrl -OutFile $tmpMsiPath
$FileName = $PwshAssetUrl | Split-Path -Leaf
$FileNameSansExt = $FileName -replace [regex]::Escape('.msi'),''
if ($Silent) {
$DateStamp = Get-Date -Format yyyyMMddTHHmmss
$MSIParentDir = $tmpMsiPath | Split-Path -Parent
$LogFilePath = Join-Path $MSIParentDir ($FileNameSansExt + '_' + $DateStamp + '.log')
$MSIArguments = @(
'/i'
$tmpMsiPath
'/qn'
'/norestart'
'/L*v'
$LogFilePath
)
try {
$null = Start-Process msiexec.exe -ArgumentList $MSIArguments -Wait -NoNewWindow
}
catch {
Write-Error $_
}
finally {
$null = Remove-Item $tmpMsiPath
}
}
else {
try {
Start-Process -Path $tmpMsiPath -Wait -NoNewWindow
}
catch {
Write-Error $_
}
finally {
Remove-Item $tmpMsiPath
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment