Coming Soon: GET-IT: Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET-IT: Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET
Exchange Server

How to Connect to Exchange Online with PowerShell

Every Microsoft Exchange administrator knows that one of the best way to get things done in Exchange on-premise is through the Exchange Management Shell, which is based on Windows PowerShell. With that in mind, many administrators may think you cannot use PowerShell to control Exchange Online, but that simply isn’t the case. You can still put all of your knowledge of scripts and cmdlets to good use with Exchange Online. In this article, I’ll show you how to connect to Exchange Online with PowerShell.

PowerShell and Exchange Online Pre-Requisites

Before starting this process, make sure that you’re running a compatible operating system with necessary updates. Any version of Windows 8 or Windows Server 2012 will work fine. You can also use Windows 7 SP1 or Windows Server 2008 R2 SP1, as long as you also install Microsoft .NET Framework 4.5 or 4.5.1 and Windows Management Framework 3.0 or 4.0.

Connecting to Exchange Online with PowerShell

1. Get started by opening Windows PowerShell on your computer. The first command you are going to enter will prompt you for your credentials:

$UserCredential = Get-Credential 

2. The Windows PowerShell Credential Request dialog box will come up.  Enter you Exchange Online user name and password and select OK. Your credentials are now stored in the variable $UserCredential, which we will use in the next command.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

Using the $UserCredential = Get-Credential PowerShell command

Using the $UserCredential = Get-Credential command to prompt the
Windows PowerShell credential request dialog box. (Image: Jeff James)

3. Next, enter the following command to create a new session  to the Microsoft Exchange Online server:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection

This last command will import commands from the remote session we established into the current session.

Import-PSSession $Session 

Using PowerShell Cmdlets with Office 365

Now you should be connected, and you should have the ability to use cmdlets up against your Office 365. Now that we have the Exchange Online commands let’s try them out. Let’s start off with something simple:


If you received an error message, then there a few things you should check:

  • Double-check that you entered your password correctly.
  • Your account must be enabled for Remote Shell.
  • There can only be three open remote PowerShell connections at a time.
  • Traffic must be allowed on port 80 between your computer and Exchange Online.

Another thing to check is to make sure that Windows PowerShell is configured to run scripts. This only needs to be done once, but you will get an error message without first configuring this option. Start by opening an elevated Windows PowerShell window (right-click the icon and select Run as Administrator). Now enter the following command:

Set-ExecutionPolicy RemoteSigned 

Choose Yes by typing ‘Y’ and pressing Enter.

Of course, when you are all done don’t simply close the PowerShell window, but make sure to disconnect first with this command:

Remove-PSSession $Session 

And there you have it.  Once connected you’ll find that there are still limitations. Being that you aren’t able to control Server features (for the most part) even through the GUI you will also be limited in that regard within the Shell. But now that you have the connection you’ll find out soon enough what you can and cannot do.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: