Maintaining 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.

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.

Advertisements

5 Replies to “Maintaining 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 https://docs.microsoft.com/en-us/powershell/module/powershellget/update-module?view=powershell-7

      -Scope
      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’.”

Leave a Reply

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