# get the URL for the latest PowerShell 6 release $url = "https://github.com/PowerShell/PowerShell/releases/latest?dummy=$(Get-Random)" $request = [System.Net.WebRequest]::Create($url) $request.AllowAutoRedirect=$false $response = $request.GetResponse() $realURL = $response.GetResponseHeader("Location") $response.Close() $response.Dispose()
# get the current version from that URL $v = ($realURL-split'/v')[-1]
# create the download URL for the release of choice # (adjust the end part to target the desired platform, architecture, and package format) $platform = "win-x64.zip" $static = "https://github.com/PowerShell/PowerShell/releases/download" $url = "$static/v$version/PowerShell-$version-$platform"
这段代码生成 ZIP 格式的 64 位 Windows 版下载链接。如果您需要不同的发行版,只需要调整 $platform 中定义的平台部分。
当获得了下载链接,您可以通过它自动完成剩下的步骤:下载 ZIP 文件,取消禁用并解压,然后执行 PowerShell 6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# define the place to download to $destinationFile = "$env:temp\PS6\powershell6.zip" $destinationFolder = Split-Path-Path$destinationFile
# create destination folder if it is not present $existsDestination = Test-Path-Path$destinationFolder if ($existsDestination-eq$false) { $null = New-Item-Path$destinationFolder-Force-ItemType Directory }