Connecting to Office 365 using PowerShell: updated
Cogmotive Reports is now Radar Reporting! Same great reporting application, but a brand new name and look. Find out more here.
The most powerful way to manage Office 365 is by using PowerShell, a command line interface that connects to Office 365 via the Internet.
Whilst it may seem daunting to people unfamiliar with working on the Command Line, it really isn’t as hard as it looks. This blog post will guide you through the basics of connecting to PowerShell.
Set up your computer to use Office 365 PowerShell
Firstly, you need to set up your computer with the necessary PowerShell modules. This only needs to be done once, however you need to have administrative permissions on the computer.
Unfortunately, Microsoft has made this part very confusing, as there are multiple versions of the PowerShell module available. The newest version is known as the Azure AD PowerShell module and is distributed via the PowerShell Gallery. This unfortunately means that you cannot download the module directly. Instead, you will need to use the PowerShellGet module, which might not be available on your system. In this case, you will have to install the module by using one of the methods detailed in this article.
An older version of the module, known as the Windows Azure Active Directory PowerShell or MSOnline module is also available. Like the Azure AD module, it’s also being distributed via the PowerShell Gallery, however an MSI installer version can be downloaded from here.
Both the Azure AD and MSOnline modules also have a Preview version, further contributing to the confusion. What’s even worse, some functionalities are only available in specific module versions, thus it might be necessary to have multiple versions installed and to use them interchangeably.
Connecting PowerShell to Office 365
Regardless of which version of the module you install, connecting to Office 365 is performed by executing a cmdlet. To connect via the Azure AD module, use:
To connect via the older MSOnline module, use:
You will be prompted for credentials. Enter the full UPN (User principal name) value of your Office 365 administrator account as well as your password. Depending on the settings you have configured, you might be asked to perform additional verification via Azure MFA.
Connecting PowerShell to Exchange Online
To connect PowerShell to Exchange Online, you will need to configure the execution policy to allow execution of signed PowerShell script. You can find detailed steps in this article. You need to perform them only once on each machine you will be connecting from.
Once the execution policy is configured, you need to create a connection to Office 365. You can do this by typing (or copying/pasting) the following into PowerShell.
Note: To paste into PowerShell you use Right Click. Here is a great video on how to use Copy and Paste in PowerShell.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential (Get-Credential) -Authentication Basic -AllowRedirection
Once this is in the PowerShell window, press Enter to execute the command.
Now you simply type in the username and password for an Administrator account in your Office 365 and click OK.
You will probably see some warnings. Don’t worry, they are normal.
You are now authenticated into Office 365 and have a session open. The last step is to fetch all the available cmdlets by using the following PowerShell command:
Congratulations! You’re connected!
Now you’ve gone to all the trouble of connecting to PowerShell, let’s do something to prove that it works. Type the following into the PowerShell window and press Enter:
You should now see a list of all the users in your Office 365 account that have mailboxes!
Cogmotive is the leading global provider of enterprise level reporting and analytics applications for Office 365. Find out more now.