In a previous article I explained how to connect to Office 365 with PowerShell. In this article we explore how to use PowerShell to connect to Exchange Online. Let’s get started!
Before you get started your computer needs to be running:
- Windows 7 Service Pack 1 or newer.
- Server 2008 R2 Service Pack 1 or newer.
You will also need to install the .NET Framework 4.5. This comes included with Windows 8 and Server 2012. For older operating systems it can be downloaded from here.
You will also need the Windows Management Framework 4.0.
Connect PowerShell to Exchange Online
I always recommend running PowerShell as an administrator. To do that, right click on the PowerShell icon and select Run As Administrator from the context menu.
First we need to set the execution policy.
C:\> Set-ExecutionPolicy RemoteSigned
Next we need to store our Office 365 credentials in a variable. Type the command below and hit enter.
C:\> $UserCredential = Get-Credential
A dialog box will appear. Type in your Office 365 credentials and click Ok.
Now let’s connect. In the command below we put our connection info into a variable. This results in less typing later.
C:\> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Finally, let’s use that variable to connect to Exchange Online and import all Exchange cmdlets into our session.
C:\> Import-PSSession $Session
You are now connected. To test your connection try running an Exchange command like Get-Mailbox.
When you are done, be sure to end your remote session with Remove-PSSession $Session.
We’d like to hear from you! How has your experience been with connecting powershell to Exchange Online? Or perhaps, connecting to Office 365 and Azure Active Directory?