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.
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 https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
This last command will import commands from the remote session we established into the current 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:
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:
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.
More in Exchange Server
Microsoft Warns About New Zero-Day Vulnerabilities in Exchange Server
Sep 30, 2022 | Rabia Noureen
Microsoft Exchange Servers Hit By Stealthy IIS Backdoors
Jul 27, 2022 | Rabia Noureen
Kaspersky Discloses New 'SessionManager' Backdoor Targetting Microsoft Exchange Servers
Jul 1, 2022 | Rabia Noureen
M365 Changelog: (Updated) Safe Links Global Settings Migrated to Custom Policies
Jun 28, 2022 | Petri Staff
Microsoft Postpones the Release of Next Version of Exchange Server Until 2025
Jun 3, 2022 | Rabia Noureen
Most popular on petri