Have you ever wondered how to import and use the Office 365 PowerShell module? In this guide, I’ll walk you through the steps to get started with using PowerShell to manage your Office 365 environment.
Step 1: Install the Office 365 PowerShell Module
Installing the module is the first step to importing and using Office 365 PowerShell Module. Follow the steps below to install the MSOnline module on your PC.
- Search powershell. Windows PowerShell will be highlighted as the best match.
On the details pane, click Run as administrator. Windows will request that you confirm PowerShell to make changes to your Windows 11 – click Yes. - Then, enter the command below on the opened Windows PowerShell command console; run the command by pressing Enter on your keyboard.
powershell.exe -ExecutionPolicy Unrestricted
- Next stop: run the command below to download and install the MSOnline module. Then, run the command beneath it to import the module into your computer.
Install-Module -Name MSOnline
Import-Module MSOnline
The command takes a short while to complete. When it completes, PowerShell returns to its prompt.
- Finally, before you proceed to the next section, confirm that you’ve installed the MSOnline module on your computer by running the command below:
Get-Module MSOnline | Select-Object -ExpandProperty ExportedCommands
The command displays all the cmdlets in the Office 365 PowerShell module. Here is an alternative command that returns the same result.
Get-Command -Module MSOnline
If you see a list of commands similar to what I have in my screenshots below, you’ve successfully imported the Office 365 PowerShell module. The first screenshot is the result of the first command above, while the second screenshot shows the results of the second command.
In the next section, you’ll prep the credentials you require to connect to Office 365. Then, in the subsequent sections, you’ll connect to 365 and use the imported O365 modules to perform some admin tasks.
Step 2: Prepare Your UserName and Password
In the next section, you will connect Office 365, but before you do that, it is a good idea to prepare the credentials you require.
Before you proceed, create a folder to save your encrypted username and password. Then, follow the steps below.
- Run the command below to encrypt your Office 365 username and password into an XML file.
Get-Credential [email protected] | Export-CliXml -Path D:\PowerShellCred\O365loginCred.xml
Windows PowerShell will display a login window when you run the above command. Enter your Office 365 password, then click OK.
- Finally, import the saved password into a variable by running the command below:
$O365loginCred = Import-Clixml D:\PowerShellCred\O365loginCred.xml
Step 3: Import O365 PowerShell Module
- Run the command below to connect to your Office 365 account using PowerShell.
Connect-MsolService -Credential $O365loginCred
When you run the command, PowerShell returns almost immediately, giving the impression that nothing happened! If PowerShell did not return any error messages, I can assure you that you’ve successfully connected to your Office 365 account.
To confirm this, let’s perform some tasks using some of the commands in the imported Office 365 PowerShell module (MSOnline).
- Return user information with the Get-MsolUser command. Once you have connected to Office 365 by running the Connect-MsolService command, you can return information about users by running the Get-MsolUser command.
If you run the Get-MsolUser command without adding any filtering, it returns ALL your Office 365 users.
The screenshot below shows the result of the Get-MsolUser command when I run the command on my Office 365 account.
DO NOT run this command in a production account with so many users. It may take too long to run.
- Return licensed Office 365 users. In the last example, the Get-MsolUser command returned all users in my O365 account.
If you examine my previous screenshot closely, you’ll notice that the command returned a column called isLicensed. The isLicensed column returns True for users that have been assigned an Office 365 license.
On the contrary, if a user has not been assigned a license, the isLicensed column returns False.
Based on this information, I can modify my previous command to return users assigned O365 licenses by running the command below:
Get-MsolUser | Where-Object {$_.isLicensed -eq "True"}
The command now returns only users assigned.
- Return unlicensed Office 365 users using the UnlicensedUsersOnly parameter of the Get-MsolUser command. In the last example, I showed you how to return only licensed users by pipping the output of Get-MsolUser to the Where-Object command.
It may interest you to know that to return unlicensed users, you do not need to pipe Get-MsolUser to the Where-Object command! This is because the Get-MsolUser cmdlet has a parameter called UnlicensedUsersOnly.
If you include this parameter, PowerShell returns all Office 365 users that have not been licensed. How cool is that!
Here is the command and the result.
Get-MsolUser -UnlicensedUsersOnly
- Return all Office 365 groups using the Get-MsolGroup command. In this last example, I want to show you how to return all groups (including shared mailboxes) to your Office 365 account.
To return ALL groups, run the Get-MsolGroup command without any parameters or filtering…
Frequently Asked Questions
The first step to using an imported PowerShell module is to find the cmdlets in the module. Run the Get-Module command with the name of the module.
Once you list the cmdlets in the imported module, use the Get-Help followed by the name of the cmdlet to learn how to use the cmdlet. For example, Get-help Get-MsolGroup displays information about the Get-MsolGroup command.
You may also get some examples by adding the Examples parameter. For instance, Get-help Get-MsolGroup -Examples.
To use Office 365 PowerShell module, follow the steps below:
a) Install the MSOnline module by running the Install-Module -Name MSOnline command.
b) Then, import the modules by running the Import-Module MSOnline command.
c) Finally, connect to Office 365 using the Connect-MsolService command.
Once you’re connected, you can manage office 365 by running commands using the cmdlets available in the MSOnline module.
Follow the steps below to install the AzureAD PowerShell module, then connect to Azure Active Directory and use the module to manage your tenant.
a) Run PowerShell as administrator, then run the Install-Module AzureAD command to install the Module.
b) Then, to import the installed modules on your computer, run the Import-Module AzureAD command.
c) To connect to Azure Active Directory, run the Connect-AzureAD command; then run the PowerShell commands in the AzureAD module
To return all Office 365 groups in PowerShell, import the Office 365 module by running the Install-Module -Name MSOnline command. Then, use the Connect-MsolService command to connect to your Office 365 account.
Finally, to return all groups in your Office 365 account, run the Get-MsolGroup command.
Importing a module downloads the module to your computer while executing it means running commands in the module.
Conclusion
Working with Office 365 Powershell is that straightforward! I hope I made your day.
I hope you found this guide helpful. If you did, let us know by sparing two minutes to share your experience vis the “Was this page helpful?” question below.
Love this guide? Get more PS guides from our Windows PowerShell page.