How to Connect PowerShell to Office 365
Required Tools
In order for you to manage your Office 365 account, you will need to download and install a few tools. You can manage your account from any Windows PC, you don’t need to install them to a server so can manage accounts directly from your PC or laptop.
Download and install each of the following tools:
Microsoft Online Services Sign-in Assistant
http://www.microsoft.com/en-us/download/details.aspx?id=41950
Azure Active Directory Module
http://go.microsoft.com/fwlink/p/?linkid=236297
SharePoint Online Module
https://www.microsoft.com/en-us/download/details.aspx?id=35588
Skype for Business Online Module
http://www.microsoft.com/en-us/download/details.aspx?id=39366
Once everything is installed open up Windows PowerShell from the start menu and run it as administrator.

The PowerShell window opens up and we’re in business!

Connecting to Office 365
When you start out you only need three commands to connect to the 365 portal. The first will prompt you for the login details of a global administrator and save them into the $credential variable, the second will import the basic 365 PowerShell commands and the third will connect you up using the saved login credentials.
$credential = Get-Credential

Import-Module MSOnline
Connect-MsolService -Credential $credential

Next you will need to update the PowerShell Execution policy so that some of the commands will run correctly. The following commands will allow you to run remote signed scripts.
Set-ExecutionPolicy “RemoteSigned” -Scope Process -Confirm:$false
Set-ExecutionPolicy “RemoteSigned” -Scope CurrentUser -Confirm:$false
If you want to manage Skype for Business Online, you will need to import the Lync Online module and import the session.
Import-Module LyncOnlineConnector
$session = New-CsOnlineSession -Credential $credential
Import-PSSession $session

If you want to manage SharePoint you will need to run the following:
Import-Module Microsoft.Online.Sharepoint.PowerShell
Connect-SPOService -url https://office365laba-admin.sharepoint.com -Credential $credential

And finally if you want to manage Exchange online you will need to run the following to create and import an Exchange session.
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $credential -Authentication “Basic” –AllowRedirection
Import-PSSession $session

Once you have all of the modules loaded you can list all of the available commands by running: Get-Command it’s a good idea to browse over the available commands and familiarize yourself with them. There have been plenty of times where I’ve seen a command that looks interesting and then I go on to use it in a script.

Summary
Connecting PowerShell to Office 365 is relatively straight forward and opens up lots of new opportunities. If you work with Office 365 as part of your job then learning to use PowerShell is a great way to get ahead of your peers and make your life easier. Follow the blog to get lots of helpful tips on managing Office 365 using PowerShell, its what I do every day so there should be something for everyone.
[email-download download_id=”9801″ contact_form_id=”9351″]If you want to get stuck in then you can buy my book from Amazon either the paper back or kindle version. My advice is to buy the paperback version so you can keep it on your desk for easy reference!
22nd December 2016
Hi,
I noticed you are using the older MSOnline PowerShell module in your examples. It may be useful to start using the newer Azure Active Directory PowerShell V2 module instead, as we will begin deprecating the MSOnline module when we have migrated the functionality of the MSOnline module to the newer module – currently planned for the Spring of 2017.
Thanks,
Rob de Jong
22nd December 2016
Thanks Rob!
I’ll definitely update the examples here asap.
Thanks for stopping by 🙂
3rd July 2018
Have you ever updated the examples?
I started following and found that Import-Module MSOnline no longer works.