Difficulties can occur when updating the set of PowerShell modules used to manage Microsoft 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 URLrename.
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 seems appropriate to write a script to:
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 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", "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 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.
Figure 1: Update PowerShell Modules for Microsoft 365 workloads
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. Microsoft updates the MicrosoftTeams and SharePoint Online management modules monthly. 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.
-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.
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?
Loading...
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.
Loading...
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’.”
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)
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
Here’s where I ran those two lines from the script manually. The $currentversion variable does get populated, but the error seems to be generated by the $currentversion.major.tostring() part. Maybe I’m missing something obvious.
Did you try the altered code that I posted? The change I made checks to see if a current version is on the PC (it might not be) and returns a version of 9.0.0 if a module is not present.
Loading...
Yes, I did try that. Same error. Like I said, when I run that line by itself, it doesn’t seem to recognize the “Major” keyword as valid. I’m getting the version number in $currentversion, but it doesn’t like the line where the script breaks it up into Major, Minor, & Build. Are those variables version specific, are they defined in a function elsewhere?
Loading...
The version information should come from the PowerShell module that’s registered on your PC. Can you run a Get-Module for the module that it’s failing for and examine what comes back?
Loading...
I get the same error for each module that it checks. If it’s working for you and not for me, it must be something on my side. I’m researching the source of “major”,”minor” and “build” to see where they come from. That is what’s failing, not the overall script itself.
Loading...
Apparently the issue was that the $currentversion variable was not cast as [version]. Once I added that, I got no errors. Thanks so much for your script and your assistance.
Loading...
I also added some code to check if any of the [version] numbers came back as negative (no value). Otherwise, you get this error:
$orcaVersion = $orcaVersion.Major.toString() + “.” + $orcaVersion.Minor.toString() + “.” + $orcaVersion.Build.toString()
Cannot convert value “2.1.-1” to type “System.Version”. Error: “Version’s parameters must be greater than or equal to zero.
Parameter name: build”
At line:1 char:1
+ $orcaVersion = $orcaVersion.Major.toString() + “.” + $orcaVersion.Min …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
{"id":null,"mode":"button","open_style":"in_modal","currency_code":"EUR","currency_symbol":"\u20ac","currency_type":"decimal","blank_flag_url":"https:\/\/office365itpros.com\/wp-content\/plugins\/tip-jar-wp\/\/assets\/images\/flags\/blank.gif","flag_sprite_url":"https:\/\/office365itpros.com\/wp-content\/plugins\/tip-jar-wp\/\/assets\/images\/flags\/flags.png","default_amount":100,"top_media_type":"featured_image","featured_image_url":"https:\/\/office365itpros.com\/wp-content\/uploads\/2022\/11\/cover-141x200.jpg","featured_embed":"","header_media":null,"file_download_attachment_data":null,"recurring_options_enabled":true,"recurring_options":{"never":{"selected":true,"after_output":"One time only"},"weekly":{"selected":false,"after_output":"Every week"},"monthly":{"selected":false,"after_output":"Every month"},"yearly":{"selected":false,"after_output":"Every year"}},"strings":{"current_user_email":"","current_user_name":"","link_text":"Virtual Tip Jar","complete_payment_button_error_text":"Check info and try again","payment_verb":"Pay","payment_request_label":"Office 365 for IT Pros","form_has_an_error":"Please check and fix the errors above","general_server_error":"Something isn't working right at the moment. Please try again.","form_title":"Office 365 for IT Pros","form_subtitle":null,"currency_search_text":"Country or Currency here","other_payment_option":"Other payment option","manage_payments_button_text":"Manage your payments","thank_you_message":"Thank you for supporting the work of Office 365 for IT Pros!","payment_confirmation_title":"Office 365 for IT Pros","receipt_title":"Your Receipt","print_receipt":"Print Receipt","email_receipt":"Email Receipt","email_receipt_sending":"Sending receipt...","email_receipt_success":"Email receipt successfully sent","email_receipt_failed":"Email receipt failed to send. Please try again.","receipt_payee":"Paid to","receipt_statement_descriptor":"This will show up on your statement as","receipt_date":"Date","receipt_transaction_id":"Transaction ID","receipt_transaction_amount":"Amount","refund_payer":"Refund from","login":"Log in to manage your payments","manage_payments":"Manage Payments","transactions_title":"Your Transactions","transaction_title":"Transaction Receipt","transaction_period":"Plan Period","arrangements_title":"Your Plans","arrangement_title":"Manage Plan","arrangement_details":"Plan Details","arrangement_id_title":"Plan ID","arrangement_payment_method_title":"Payment Method","arrangement_amount_title":"Plan Amount","arrangement_renewal_title":"Next renewal date","arrangement_action_cancel":"Cancel Plan","arrangement_action_cant_cancel":"Cancelling is currently not available.","arrangement_action_cancel_double":"Are you sure you'd like to cancel?","arrangement_cancelling":"Cancelling Plan...","arrangement_cancelled":"Plan Cancelled","arrangement_failed_to_cancel":"Failed to cancel plan","back_to_plans":"\u2190 Back to Plans","update_payment_method_verb":"Update","sca_auth_description":"Your have a pending renewal payment which requires authorization.","sca_auth_verb":"Authorize renewal payment","sca_authing_verb":"Authorizing payment","sca_authed_verb":"Payment successfully authorized!","sca_auth_failed":"Unable to authorize! Please try again.","login_button_text":"Log in","login_form_has_an_error":"Please check and fix the errors above","uppercase_search":"Search","lowercase_search":"search","uppercase_page":"Page","lowercase_page":"page","uppercase_items":"Items","lowercase_items":"items","uppercase_per":"Per","lowercase_per":"per","uppercase_of":"Of","lowercase_of":"of","back":"Back to plans","zip_code_placeholder":"Zip\/Postal Code","download_file_button_text":"Download File","input_field_instructions":{"tip_amount":{"placeholder_text":"How much would you like to tip?","initial":{"instruction_type":"normal","instruction_message":"How much would you like to tip? Choose any currency."},"empty":{"instruction_type":"error","instruction_message":"How much would you like to tip? Choose any currency."},"invalid_curency":{"instruction_type":"error","instruction_message":"Please choose a valid currency."}},"recurring":{"placeholder_text":"Recurring","initial":{"instruction_type":"normal","instruction_message":"How often would you like to give this?"},"success":{"instruction_type":"success","instruction_message":"How often would you like to give this?"},"empty":{"instruction_type":"error","instruction_message":"How often would you like to give this?"}},"name":{"placeholder_text":"Name on Credit Card","initial":{"instruction_type":"normal","instruction_message":"Enter the name on your card."},"success":{"instruction_type":"success","instruction_message":"Enter the name on your card."},"empty":{"instruction_type":"error","instruction_message":"Please enter the name on your card."}},"privacy_policy":{"terms_title":"Terms and conditions","terms_body":null,"terms_show_text":"View Terms","terms_hide_text":"Hide Terms","initial":{"instruction_type":"normal","instruction_message":"I agree to the terms."},"unchecked":{"instruction_type":"error","instruction_message":"Please agree to the terms."},"checked":{"instruction_type":"success","instruction_message":"I agree to the terms."}},"email":{"placeholder_text":"Your email address","initial":{"instruction_type":"normal","instruction_message":"Enter your email address"},"success":{"instruction_type":"success","instruction_message":"Enter your email address"},"blank":{"instruction_type":"error","instruction_message":"Enter your email address"},"not_an_email_address":{"instruction_type":"error","instruction_message":"Make sure you have entered a valid email address"}},"note_with_tip":{"placeholder_text":"Your note here...","initial":{"instruction_type":"normal","instruction_message":"Attach a note to your tip (optional)"},"empty":{"instruction_type":"normal","instruction_message":"Attach a note to your tip (optional)"},"not_empty_initial":{"instruction_type":"normal","instruction_message":"Attach a note to your tip (optional)"},"saving":{"instruction_type":"normal","instruction_message":"Saving note..."},"success":{"instruction_type":"success","instruction_message":"Note successfully saved!"},"error":{"instruction_type":"error","instruction_message":"Unable to save note note at this time. Please try again."}},"email_for_login_code":{"placeholder_text":"Your email address","initial":{"instruction_type":"normal","instruction_message":"Enter your email to log in."},"success":{"instruction_type":"success","instruction_message":"Enter your email to log in."},"blank":{"instruction_type":"error","instruction_message":"Enter your email to log in."},"empty":{"instruction_type":"error","instruction_message":"Enter your email to log in."}},"login_code":{"initial":{"instruction_type":"normal","instruction_message":"Check your email and enter the login code."},"success":{"instruction_type":"success","instruction_message":"Check your email and enter the login code."},"blank":{"instruction_type":"error","instruction_message":"Check your email and enter the login code."},"empty":{"instruction_type":"error","instruction_message":"Check your email and enter the login code."}},"stripe_all_in_one":{"initial":{"instruction_type":"normal","instruction_message":"Enter your credit card details here."},"empty":{"instruction_type":"error","instruction_message":"Enter your credit card details here."},"success":{"instruction_type":"normal","instruction_message":"Enter your credit card details here."},"invalid_number":{"instruction_type":"error","instruction_message":"The card number is not a valid credit card number."},"invalid_expiry_month":{"instruction_type":"error","instruction_message":"The card's expiration month is invalid."},"invalid_expiry_year":{"instruction_type":"error","instruction_message":"The card's expiration year is invalid."},"invalid_cvc":{"instruction_type":"error","instruction_message":"The card's security code is invalid."},"incorrect_number":{"instruction_type":"error","instruction_message":"The card number is incorrect."},"incomplete_number":{"instruction_type":"error","instruction_message":"The card number is incomplete."},"incomplete_cvc":{"instruction_type":"error","instruction_message":"The card's security code is incomplete."},"incomplete_expiry":{"instruction_type":"error","instruction_message":"The card's expiration date is incomplete."},"incomplete_zip":{"instruction_type":"error","instruction_message":"The card's zip code is incomplete."},"expired_card":{"instruction_type":"error","instruction_message":"The card has expired."},"incorrect_cvc":{"instruction_type":"error","instruction_message":"The card's security code is incorrect."},"incorrect_zip":{"instruction_type":"error","instruction_message":"The card's zip code failed validation."},"invalid_expiry_year_past":{"instruction_type":"error","instruction_message":"The card's expiration year is in the past"},"card_declined":{"instruction_type":"error","instruction_message":"The card was declined."},"missing":{"instruction_type":"error","instruction_message":"There is no card on a customer that is being charged."},"processing_error":{"instruction_type":"error","instruction_message":"An error occurred while processing the card."},"invalid_request_error":{"instruction_type":"error","instruction_message":"Unable to process this payment, please try again or use alternative method."},"invalid_sofort_country":{"instruction_type":"error","instruction_message":"The billing country is not accepted by SOFORT. Please try another country."}}}},"fetched_oembed_html":false}
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
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.
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?
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.
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’.”
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)
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?
Any thoughts on this error:
$CurrentVersion = $CurrentVersion.Major.toString() + “.” + $CurrentVersion.Minor.toString() + “.” + $CurrentVersion.Build.toString()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $CurrentVersion = $CurrentVersion.Major.toString() + “.” + $CurrentVe …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Is this a Powershell version thing?
There’s some issue with the $CurrentVersion variable. What is its value?
This code might be better:
$CurrentVersion = (Find-Module -Name $Module | Select-Object -ExpandProperty Version)
If ($CurrentVersion) {
$CurrentVersion = $CurrentVersion.Major.toString() + “.” + $CurrentVersion.Minor.toString() + “.” + $CurrentVersion.Build.toString()
Write-Host (“Current version of the {0} module in the PowerShell Gallery is {1}” -f $Module, $CurrentVersion)
} Else {
$CurrentVersion = “0.0.0”
Write-Host (“No module found for {0}” -f $Module) }
Here’s where I ran those two lines from the script manually. The $currentversion variable does get populated, but the error seems to be generated by the $currentversion.major.tostring() part. Maybe I’m missing something obvious.
$CurrentVersion = (Find-Module -Name MicrosoftTeams | Select-Object -ExpandProperty Version)
$currentversion
4.9.1
$CurrentVersion = $CurrentVersion.Major.toString() + “.” + $CurrentVersion.Minor.toString() + “.” + $CurrentVersion.Build.toString()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $CurrentVersion = $CurrentVersion.Major.toString() + “.” + $CurrentVe …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Did you try the altered code that I posted? The change I made checks to see if a current version is on the PC (it might not be) and returns a version of 9.0.0 if a module is not present.
Yes, I did try that. Same error. Like I said, when I run that line by itself, it doesn’t seem to recognize the “Major” keyword as valid. I’m getting the version number in $currentversion, but it doesn’t like the line where the script breaks it up into Major, Minor, & Build. Are those variables version specific, are they defined in a function elsewhere?
The version information should come from the PowerShell module that’s registered on your PC. Can you run a Get-Module for the module that it’s failing for and examine what comes back?
I get the same error for each module that it checks. If it’s working for you and not for me, it must be something on my side. I’m researching the source of “major”,”minor” and “build” to see where they come from. That is what’s failing, not the overall script itself.
Apparently the issue was that the $currentversion variable was not cast as [version]. Once I added that, I got no errors. Thanks so much for your script and your assistance.
I also added some code to check if any of the [version] numbers came back as negative (no value). Otherwise, you get this error:
$orcaVersion = $orcaVersion.Major.toString() + “.” + $orcaVersion.Minor.toString() + “.” + $orcaVersion.Build.toString()
Cannot convert value “2.1.-1” to type “System.Version”. Error: “Version’s parameters must be greater than or equal to zero.
Parameter name: build”
At line:1 char:1
+ $orcaVersion = $orcaVersion.Major.toString() + “.” + $orcaVersion.Min …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException