At the start of this year I posted a PowerShell Challenge where I showed you how I created a script that would Check for PowerShell Core updates (Now just called PowerShell 😀 ) and install them as needed. Today I have published the cleaned up version of this script to the PowerShell Gallery!
The goal of the script
This script can be used as a scheduled task on a Windows device. It gets the metadata for the latest PowerShell release and compares it to the local installation. If there is a newer version available, it shows a popup asking if you want to update. If you do, it will download and install, using a one liner from Thomas’ blog, that calls for the script provided in the PowerShell Github . It will do this for both the regular version and preview.
Getting the script
Since the script is in the gallery, you can now install it with one little cmdlet:
Install-Script -Name Get-PwshUpdate
This will save the script to your PowerShell folder, where you can find it by using Get-InstalledScript.
Want to save the script to another location?
Just use
Save-Script -Name Get-PwshUpdate -Path C:\temp
Creating the scheduled task
For the scheduled task, I have created a little snippet to install it. Change the Checktime to your needs. If you have used Save-Script, you want to change the ScriptPath as well.
Run this as an administrator:
# Run as administrator! # Run in Windows PowerShell, this does not work in Core $InstalledScriptPath = (Get-InstalledScript -Name Get-PwshUpdate).InstalledLocation $ScriptPath = "$InstalledScriptPath\Get-PwshUpdate.ps1" #Format as Date-Time [DateTime]$CheckTime = "10pm" $Parameters = @{ "Execute" = "Powershell.exe" "Argument" = "-ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -file `" $ScriptPath`" " } $Action = New-ScheduledTaskAction @Parameters $Trigger = New-ScheduledTaskTrigger -Daily -At $CheckTime $Parameters = @{ "Action" = $Action "Trigger"= $Trigger "TaskName" = "PWSH Update check" "RunLevel" = "Highest" "Description" = "Daily check for PWSH updates" } Register-ScheduledTask @Parameters
Results
So the result will be a scheduled task that does a check for PowerShell updates. If they are there, it will ask you if you want to install a new version.
(this gif is sped up a bit. It takes a bit more time in real life as the MSI-file needs to download.)
After that, it will download and present you with the menu to install.
I have chosen to not install silently, as I always like to know what’s going on on my computer. Maybe this is something that could be added at a later time.
In a later post, I will dive a little deeper into the changes I have made to make this script gallery-ready. Special thanks to Gerbrand for giving the code review.
If you experience any issues with the script, please don’t hesitate to leave a comment or create an issue.
Pingback:ICYMI: PowerShell Week of 5-July-2019 | PowerShell.org
Pingback:Dowst.Dev | PowerShell Weekly – July 5, 2019