Important to Update PowerShell Modules Regularly
Updated: 15 August 2023
It’s important to keep PowerShell modules updated because Microsoft introduces new cmdlets and cmdlet parameters to support new functionality, like SharePoint Online site URL rename. Or when Microsoft updates the Microsoft Graph PowerShell SDK or MicrosoftTeams modules, both of which receive updates on a monthly (or even more frequent) basis.
Scripting PowerShell Updates
Most of the important PowerShell modules used with Microsoft 365 are now available in the PowerShell Gallery and can be installed and updated from there. It seemed appropriate to write a script to:
- Define a set of modules that I’m interested in updating, such as the Exchange Online Management module, Microsoft Teams, the Microsoft Graph PowerShell SDK, ORCA (used to check Advanced Threat Protection Settings) and so on.
- Check the PowerShell Gallery to see if an update is available, and if so, download and install it.
- Clean up afterwards by checking the PC for versions of a module and removing all but the latest to ensure that scripts only ever run the latest cmdlets.
Here’s the basic processing steps for the original version of the script (I update the code on an ongoing basis to cope with changes, such as the split of the Microsoft Graph PowerShell SDK into V1.0 and beta modules from V2 onward). The script must be run after starting PowerShell as an administrator (otherwise you won’t be able to install the updates). Make sure that all other PowerShell sessions are ended to avoid the possibility of trying to update a module that’s in use.
# Define the set of modules installed and updated from the PowerShell Gallery that we want to maintain
$O365Modules = @("MicrosoftTeams", "MicrosoftTeams", "Microsoft.Graph", "ExchangeOnlineManagement", "Microsoft.Online.Sharepoint.PowerShell", "ORCA")
# Check and update all modules to make sure that we're at the latest version
ForEach ($Module in $O365Modules) {
Write-Host "Checking and updating module" $Module
Update-Module $Module -Force -Scope AllUsers }
# Check and remove older versions of the modules from the PC
ForEach ($Module in $O365Modules) {
Write-Host "Checking for older versions of" $Module
$AllVersions = Get-InstalledModule -Name $Module -AllVersions
$AllVersions = $AllVersions | Sort-Object PublishedDate -Descending
$MostRecentVersion = $AllVersions[0].Version
Write-Host "Most recent version of" $Module "is" $MostRecentVersion "published on" (Get-Date($AllVersions[0].PublishedDate) -format g)
If ($AllVersions.Count -gt 1 ) { # More than a single version installed
ForEach ($Version in $AllVersions) { #Check each version and remove old versions
If ($Version.Version -ne $MostRecentVersion) { # Old version - remove
Write-Host "Uninstalling version" $Version.Version "of Module" $Module -foregroundcolor Red
Uninstall-Module -Name $Module -RequiredVersion $Version.Version -Force
} #End if
} #End ForEach
} #End If
} #End ForEach
You can download the latest version of the script from GitHub and amend it to update the modules you use. Because I use the script to keep my PCs updated with the latest PowerShell modules, the current code is more developed and comprehensive than the snippet shown above. The script isn’t perfect, but it gets the job done for me.
Figure 1 shows some typical output as the script processes the set of Microsoft 365 PowerShell modules and updates those when a new version is available in the PowerShell gallery.
The script installs and updates modules in the $env:ProgramFiles\PowerShell\Modules location to make sure that the files are on the local workstation and available to all users. From PowerShell 6 onward, the default is to install modules in $HOME\Documents\PowerShell\Modules, which might mean that they end up in OneDrive.
Monthly Updates
Given the cadence of Microsoft updates for PowerShell modules, it’s a good idea to update PowerShell modules every month or six weeks. Perhaps reflecting the ongoing growth in Graph APIs, the Microsoft Graph PowerShell SDK receives even more frequent updates. If you use the Graph SDK with Azure Automation, make sure that you update PowerShell modules for the Graph SDK in your Azure Automation accounts too.
Checking, updating, and removing PowerShell modules is not a fast process, but it does help to keep your scripting in good health.
The Office 365 for IT Pros eBook includes extensive coverage of managing Office 365 applications with PowerShell, so we pay attention to this kind of stuff – just like you should.

