Script To Download the Current Version of PowerShell
One of the annoying things about well, any software is checking to see if you have the currrent version. This is a thing with PowerShell, and because the PowerShell team in its finite wisdom has decided to not just have a “latest stable/lts/preview” url, we have to figure this out manually.
It’s not too bad though, fortunately, the PowerShell team gives us a good way to check and a bit of simple comparison to help us determine if we have the newest version. The basic flow is “get the current installed version of PowerShell, then check to see if the current version on github is newer. If it is, download that.”
Now, there’s a couple caveats: the installed version is based on what you’re running the script in. So if you’re going between say stable and preview, if you run the version checker in stable, that will give the version for stable. You’ll have to run it in preview for that, and do your own version comparison. I’m just doing this based on stable.
To see what the current installed version of PowerShell is, we run $PSVersionTable.psversion.ToString() which gives us a nice neat value, so for stable on my machine, I get: 7.5.4
We then want to see what the current stable downloadable version is. Fortunately, the PowerShell team does us a solid by making that available from a stable URL as JSON data. For stable, the URL is: https://aka.ms/pwsh-buildinfo-stable. To check for lts or preview, change -stable to -lts or -preview. If we hit that URL, we get a json file with the following
{ "BaseUrl": "https://powershellinfraartifacts-gkhedzdeaghdezhr.z01.azurefd.net/install", "ReleaseTag": "v7.5.4", "ReleaseDate": "2025-10-20T19:26:23Z", "BlobName": "v7-5-4"}The ReleaseTag is the thing of the most interest to us. When it comes to downloading, while not giving us a “latest” URL, the PowerShell team does give us a fairly consistent URL pattern. For example, for the installer package for the Apple Silicon macOS version, the URL is: https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/powershell-7.5.4-osx-arm64.pkg, so we can see where we can use the ReleaseTag directly in the URL in between download/ and /powershell-, and then if we yank the leading v off the ReleaseTag, we can use that in the actual package name. Consistency is really handy. The script itself then is:
Get the installed versionGet the current github versionif the current github version is newer/greater than the installed version, download that package file to /tmpWe use Invoke-RestMethod as our “curl” command, since in this case, it gives us the JSON data in a usable way sans any custom parsing. From there, install it via whatever method you wish. Obviously, the script is a PowerShell script, duh. So the script:
#get the installed (stable) version$installedPSVersion = $PSVersionTable.psversion.ToString()#get the JSON data from github$PSLatestStableVersionInfo = Invoke-RestMethod -Uri "https://aka.ms/pwsh-buildinfo-stable" -Method Get#get the releasetag$PSLatestStableReleaseTag= $PSLatestStableVersionInfo.ReleaseTag#build the version sans the leading v$PSLatestStableVersion = $PSLatestStableReleaseTag.Substring(1)#test to see if github is newerif ($PSLatestStableVersion -gt $testVersion) { #github version is newer, build the download URL $macOSPowerShellASPKGDownloadURL = "https://github.com/PowerShell/PowerShell/releases/download/$PSLatestStableReleaseTag/powershell-$PSLatestStableVersion-osx-arm64.pkg" #build our download filename $macOSPowerShellASPKG = "powershell-$PSLatestStableVersion-osx-arm64.pkg" #now we download the file to /tmp Invoke-RestMethod -Uri $macOSPowerShellASPKGDownloadURL -Method Get -OutFile "/tmp/$macOSPowerShellASPKG"}
And that’s it. Run this, and when it’s finished, if the github version is newer, you’ll have that package file in /tmp (as $deity intended), ready to go for whatever your next steps are.
#automation #DownloadLatestPowerShellAutomatically #macPowershell #PowerShell #powershellFun #PowershellMac