If you are using PowerShell, you probably use some modules from the PowerShell Gallery. These modules are created by Microsoft or by third party companies, but most of them are open source and created by the community. In all cases, chances are the module gets regular updates. This could be to fix bugs, add new functionality or make changes needed for the technology the module works with. This is often good news, but it does mean that you need to manually update every module. As that takes time, I have made a script that can update all PowerShell modules on a system.
What does the script do
I have created this script years ago with my colleague Gerbrand. Recently I have updated the script and made it more flexible for everyone to use.
The script goes through all installed PowerShell modules on the system.
For each module, it checks if a new version is available in the PowerShell Gallery. If there is, it installs the update.
After that, it will remove the old versions of the module, as that is not something that the system does automatically.
I have created some parameters to help this script fit your personal needs:
If there are modules you don’t want to include in the automatic update, you can add them to this parameter.
A major update is an update where the first number of the version is changed. This means that breaking changes can be involved. By using this parameter the script will not perform an automatic update when a major change is involved. To learn more about semantic versioning, click here.
You can use this switch to stop the script from removing the old module versions.
In this parameter you can define modules where you don’t want to automatically remove the old version. I added Az as a default option in here, as I have found the module to sometimes get instable after an old version is removed.
How to run
I run the script manually about once a week. I have made a part of a personal module that I import, but you can also do it with dot sourcing. After it is loaded, you can run the Update-EveryModule cmdlet. But of course the better solution is to automated it completely! You can do that with a schedule task for example. In this post, I show how you can make one with PowerShell.
If you want to automatically update all modules in an Azure Automation account, you can do so by using the script I wrote about here.
You can find the script here
So this is how you can update all PowerShell modules on a system. If you have any questions, leave them in the comments!
* Doesn’t handle preview or nightly build modules well (eg PnP.PowerShell)
* There are official (test/preview) modules coming from other repos (eg. poshtestgallery.com)
* When updating, you might want to specify the same source repo as the one from the module you’re updating
* Before uninstalling, you might want to unload (remove) the module
* For proper version comparisons, might want to use the [System.Version] class; text-oriented version comparison leads to 9.1 being bigger than 10.0 for example.
I have a ‘connect’ script for M365 services where I had to deal with the same issues.
It will not update to new preview modules, but I think that should be a manual process anyway
This script is only meant to work for the PowerShell Gallery. I’ve added a filter to Get-InstalledModule to keep within that scope
Great script. Thanks so much!
You can update all module in one command in PowerShell
just make sure the repo is trusted to avoid “Untrusted repository question” in every module updated
Set-PSRepository -Name “repo-name” -InstallationPolicy Trusted
If you install the modules from PSGallery repo use :
Set-PSRepository -Name “PSGallery” -InstallationPolicy Trusted
then use this command to update all module :
Get-InstalledModule | Update-Module -WhatIf -AcceptLicense
-WhatIf : optionall
-AcceptLicense : optionall
That would certainly work! This script offers extra functionality, like logs, skipping majorversions and removing old module versions.
Why do people write big long complicated scripts to do what can be done in a 1-liner… :
get-installedmodule | update-module -force
This script offers extra functionality, like logs, skipping major versions, adding exclusions and removing old module versions.