How to Connect to Office 365 using Powershell. Have you ever found yourself needing to manage Office 365 users, groups, and services, but the web based admin center just doesn’t cut it? In this situation, PowerShell is a great help. In this guide, I’ll teach you how to connect to Office 365 with PowerShell in two simple steps.
After, I will show you how to display all available commands for managing O365, and finally, I have some examples of tasks you can perform in Office 365 with PowerShell.
Let’s start.
How to Connect to Office 365 using Powershell
Step 1: Install the MSOnline PowerShell Module
As mentioned in my introduction, the first step to connecting to Office 365 via PowerShell is to install the MSOnline module.
Follow the steps below to install the module:
1. Search powershell, then, on the search result, with Windows PowerShell selected, click Run as Administrator.
Windows will prompt you to allow the app to make changes to your PC; click Yes.
2. When the PowerShell command console opens, run the command below.
The command opens your PowerShell session with the Unrestricted ExecutionPolicy. This allows you to run commands from the module you will download later.
powershell.exe -ExecutionPolicy Unrestricted
3. Then, to install the MSOnline PowerShell module, enter the command below and press the Enter.
Import-Module MSOnline
The command may take a while to run. When it finishes, the PowerShell console returns to the prompt.
4. Before you proceed, confirm that the module has been installed on your computer by running the command below:
Get-Module MSOnline
If you successfully installed the MSOnline module on your computer, the above command displays results similar to what is in the screenshot below.
5. Finally, to display all the cmdlets (commandlets) in the MSOnline module, run the Get-Module.
(Get-Module MSOnline | Select-Object ExportedCommands).ExportedCommands
Step 2: Connect To Office 365 With the Connect-MsolService Command
Once you have installed the MSOnline module and confirmed that the cmdlets are available on your computer, proceed with the steps below to connect to Office 365.
1. Encrypt
Encrypt your Office 365 credentials and save it in a file called Office365.XML by running the command below:
Change UserName@DomainName.com to your Office 365 user name. The name MUST be specified in the UPN format, UserName@DomainName.com. Also, change the path – D:\PowerShellCred\ – to a path on your local computer.
Get-Credential UserName@DomainName.com | Export-CliXml -Path D:\PowerShellCred\Office365.XML
2. Decrypt
The next step is to decrypt the password and save it in a variable by running the Import-Clixml command as shown below:
$ImpCred = Import-Clixml D:\PowerShellCred\Office365.XML -ErrorAction SilentlyContinue
3. Connect
Finally, connect to your Office 365 tenant by running the Connect-MsolService PowerShell command below:
Connect-MsolService -Credential $ImpCred
If everything goes as expected, the command should run successfully, and PowerShell should return to its prompt.
Effectively, after running the command, it will appear that nothing happens. This means that you have successfully connected to Office 365 with PowerShell.
You can run available PowerShell commands from this point – see some examples in the next section.
Improve your Active Directory Security & Azure AD with Office 365 Reporting Tool
Try us out for Free, Access to all features. – 200+ AD Report templates Available. Easily customise your own AD reports.
Examples of Commands You Can Run After Connecting To Office 365
Now that you’ve successfully connected to your Office 365, I show you common commands you can run.
Get-MsolUser
The Get-MsolUser command is one of the commands you will likely run in Office 365. The Get-MsolUser command returns all users in your Office 365 tenant.
If you have a large user base, DO NOT run this command without filtering, as it may take too long to return all users in your account.
The screenshot below shows the result of the command against my O365.
Earlier, I advised against running the Get-MsolUser command without any parameters.
If you look closely at my screenshot above, you notice that the command result includes an isLicensed column which includes the boolean value, True or False. Based on this, you can use this command to return all users in your Office 365 tenant that are licensed.
To return all licensed users, modify the Get-MsolUser command as shown below:
Before moving on to the next example, I share one more important task you can perform with the Get-MsolUser.
Use this command to get the Office 365 users that have been assigned a specific Office license type.
If you know the license type you want to report for, run the comma below. The command returns all users that have been assigned the “ENTERPRISEPACK” license.
Replace ENTERPRISEPACK with the license type you want to return users for.
The last command works if you already know the license type. However, if you do not know the type of license available in your Office 365, run the command below to return all available licenses.
Run the command
Get-MsolAccountSku
The command returns all Office 365 licenses available in your account. Also included with the result is the number of licenses available in your account (ActiveUnits column).
Additionally, you also get how many of the licenses have been used (the ActiveUnits column).
Once you have this information, you use the license name in the command below to return the users assigned to a particular license. Get the name of the license from the AccountSkuId column of the results of the last command.
Get-MsolUser | Where-Object { $_.Licenses[0].AccountSkuId -eq "ENTERPRISEPACK" } | Select-Object UserPrincipalName, DisplayName
For my Office 365, when I run the command below for a license type, it returns the result shown in the screenshot below:
Get-MsolDomain
How to Connect to Office 365 using Powershell Conclusion
The Office 365 PowerShell module has a lot to offer. However, before you access the commands in the module, you have to connect to O365.
In this guide, I showed you the steps to connect to your Office 365 account with PowerShell and run come common commands.
Connecting to O365 via PowerShell is a simple two-step process:
- Install the MSOnline PowerShell module.
- Connect To Office 365 With the Connect-MsolService Command.
Once you have successfully connected, you list all the available cmdlets in the MSOnline PowerShell module by running the command below:
(Get-Module MSOnline | Select-Object ExportedCommands).ExportedCommands

Try InfraSOS for FREE
Invite your team and explore InfraSOS features for free
- Free 15-Days Trial
- Easy Setup
- Full Access to Enterprise Plan