How to Make Sure That You Use the Latest Office 365 PowerShell Modules

Important to Keep PowerShell Modules Updated

I’ve had some recent difficulties updating PowerShell modules used with Office 365 services. One problem happened updating the AzureADPreview module, when files were installed in OneDrive for Business. Or when the SharePoint Online module insisted on installing an update in a different location. In any case, it’s important to keep modules updated because Microsoft introduces new cmdlets and cmdlet parameters to support new functionality, like SharePoint Online site URL rename.

Scripting PowerShell Updates

Most of the important Office 365 modules are now available in the PowerShell Gallery and can be installed and updated from there. It seems appropriate to write a script to:

  • Define a set of modules that I’m interested in updating, such as the new Exchange Online Management V2 module, Azure AD, 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 script that I use. 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", "MSOnline", "AzureADPreview", "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 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 script from GitHub and amend it to update the modules you use.

Figure 1 shows some typical output as the script updates modules.

Updating PowerShell Modules
Figure 1: Updating PowerShell Modules

Monthly Updates

Given the cadence of Microsoft updates for PowerShell modules, it’s a good idea to check for updates monthly. 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.

9 Replies to “How to Make Sure That You Use the Latest Office 365 PowerShell Modules”

  1. I receive a number of scope errors when attempting to run this script. Can you provide any guidance to remedy?

    Error message syntax:

    Update-Module : A parameter cannot be found that matches parameter name ‘Scope’.

    + Update-Module $Module -Force -Scope AllUsers }
    + ~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Update-Module], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Update-Module

    1. From

      Specifies the installation scope of the module. The acceptable values for this parameter are AllUsers and CurrentUser. If Scope isn’t specified, the update is installed in the CurrentUser scope.

      The AllUsers scope requires elevated permissions and installs modules in a location that is accessible to all users of the computer:

      You’re not running PowerShell in an administrator session.

      1. Thank you for the response, Tony. I am launching Windows PowerShell ISE as administrator and have local admin rights on my PC. Any other thought? Should I try changing the value to CurrentUser if I’m only concerned with it updating for my profile?

      2. You could certainly do that… But before you do, try opening a simple PowerShell session as an admin (not ISE) to see if that works.

  2. One step ahead. I did to no avail. Also tried swapping out AllUsers for CurrentUser. with no luck. Same error complaining that “A parameter cannot be found that matches the parameter name ‘Scope’.”

  3. Sorry to reply so late. Updating Powershellget to version 2.2.3 should resolve this issue as it adds the -scope parameter to the Update-Module command.
    Hoping @Paul ticked the ‘Notify me of new comments via email’ checkbox ;p)

    1. Thanks, Matt. I did have it checked, fortunately. 🙂 Update-Module did improve the script run some but I still have a number of lines returning


      Checking and updating module MicrosoftTeams
      Update-Module : A parameter cannot be found that matches parameter name ‘Scope’.
      At C:\Users\aaa0013.BUILD\OneDrive – BMC\Documents\WindowsPowerShell\Scripts\Office 365\UpdateOffice365PowerShellModulesCurrentUser.ps1:15 char:33
      + Update-Module $Module -Force -Scope CurrentUser }
      + ~~~~~~
      + CategoryInfo : InvalidArgument: (:) [Update-Module], ParameterBindingException
      + FullyQualifiedErrorId : NamedParameterNotFound,Update-Module


      But then later in the script it returns:


      Checking for older versions of MicrosoftTeams
      Most recent version of MicrosoftTeams is 1.1.6 published on 9/14/2020 10:22 PM
      Uninstalling version 1.0.2 of Module MicrosoftTeams


      So I think it worked?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.