This PowerShell blog post will show you a single PowerShell script to install multiple M365 modules.
PowerShell management for Microsoft 365 offers a versatile toolkit for administrators seeking to streamline their workflow and automate routine tasks across the Microsoft 365 suite.
By utilizing PowerShell, administrators can efficiently manage their organization’s user accounts, licenses, and services, all from a single command-line interface.
This powerful tool enables the execution of bulk operations, such as modifying user properties or assigning licenses, which can be particularly useful in large enterprises.
About the Script
By default, the script installs the following PowerShell module (on Windows, macOS and Linux):
- ExchangeOnlineManagement
- SharePointPnPPowerShellOnline
- Azure
- MicrosoftTeams
- Microsoft.Graph
You can easily add more PowerShell modules to the list, add them to the list and run the script.
The script also includes exception handling in case there is an error.
Single PowerShell Script to Install Multiple M365 Modules
The script does the following: uninstall, install, import and check if the module has been loaded to the PowerShell session. The script has been tested on macOS and Windows.
# Define the modules and add more modules
$modules = @(
"ExchangeOnlineManagement",
"SharePointPnPPowerShellOnline",
"Azure",
"MicrosoftTeams",
"Microsoft.Graph"
)
# Uninstall the modules if they exist
foreach ($module in $modules) {
try {
if (Get-Module -ListAvailable -Name $module) {
Uninstall-Module -Name $module -AllVersions -Force -ErrorAction Stop
}
} catch {
Write-Error "Failed to uninstall module ${module}: $_" }
}
# Install the latest versions of the modules
foreach ($module in $modules) {
try {
Install-Module -Name $module -Force -ErrorAction Stop
} catch {
Write-Error "Failed to install module ${module}: $_" }
}
# Import the installed modules
foreach ($module in $modules) {
try {
Import-Module $module -ErrorAction Stop
} catch {
Write-Error "Failed to import module ${module}: $_" }
}
# Check if the modules are installed
foreach ($module in $modules) {
try {
Get-Module -ListAvailable $module -ErrorAction Stop
} catch {
Write-Error "Failed to get module ${module}: $_" }
}
If you have recommendations to improve the code, comment on this post.
Graph beta seems to come up.