This is a short article about how to install and use the MSOnline PowerShell module – also called the MSOL module, because it provides all these cmdlets like “Get-MSOLUser”. If you want to script with O365 and Azure objects (users, groups…), then MSOL is one of the important modules for you. There is another article on this blog with a comparison of the modules needed for O365 and Azure AD scripting.
To start with the MSOnline module, you should have Powershell 5.1 on the system. So you are fine on Windows 10 systetems and Windows Server 2016 and newer – on older Windows OS version you have to install Powershell 5.1. If you don’t know your current PowerShell version, use this:
Unfortunately, you cannot use the MSOnline with PowerShell Core 6 (it will crash the powershell) – and also under Powershell 7, this module is not supported. But you can use it in compatibility mode (see below).
Get it Going
Open an elevated PowerShell console (“Run as Administrator”). Then just use the Install-Module cmdlet, and don’t forget: The module name is not MSOL, it is “MSOnline”:
If you install a PowerShell module from the online repositories for the very first time, then the process informs you that the NuGet provider must be installed first, please agree here with “y”.
There might be another warning that the repository is untrusted. You can agree with “y” because the module is an official Microsoft module in the PowerShell Gallery (which is the repository).
The module is now downloaded from the PowerShell Gallery. Size is less than 10 MB, so it shouldn’t take too long…
After the module installation, you are ready to go. In your script, you always have to import the module first – then you can authenticate to your tenant:
Import-Module MSOnline Connect-MSOLService
Now you are ready to access the objects in your tenant:
MSOL with PowerShell 7
Although the MSOnline module is officially not supported on PowerShell 7, you can use a trick to do it anyway. Normally, you can install and even import the module but you cannot use it:
However, there is an easy way to use the module anyway: We import it with the compatibility mode switch.
Import-Module MSOnline -UseWindowsPowerShell
After this, you can connect to a tenant and access the objects there without problems:
The warning message that “all the objects returned by this module will be deserialized objects” can be safely ignored. That message means that in every command that returns some cloud objects like user accounts or groups, you will lose the object’s original rich type and methods.
But the data properties are readable and in almost any case that’s all you need in the modules for Azure AD object handling. There are no member methods available in a returned MSOL object, so we get all our data structures and this is enough :).
I wrote a few blog posts about typical error messages when installing and connecting the cloud PowerShell modules:
- Troubleshooting “The following commands are already available on this system”
- Troubleshooting “No match was found for the specified criteria and module name”
Microsoft provides an online reference about all cmdlets which come with the MSOL module, as well as a detailed description how to install and use the MSOL PowerShell module: