Connecting to Exchange Online for powershell

Exchange Online PowerShell allows you to manage your Exchange Online settings from the command line. You use Windows PowerShell on your local computer to create a remote PowerShell session to Exchange Online. It’s a simple three-step process where you enter your Office 365 credentials, provide the required connection settings, and then import the Exchange Online cmdlets into your local Windows PowerShell session so that you can use them.

 

Getting started is easy:

Windows PowerShell needs to be configured to run scripts, and by default, it isn’t. You’ll get the following error when you try to connect:

Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.

To require all PowerShell scripts that you download from the internet are signed by a trusted publisher, run the following command in an elevated Windows PowerShell window (a Windows PowerShell window you open by selecting Run as administrator):

 

Set-ExecutionPolicy RemoteSigned

You need to configure this setting only once on your computer, not every time you connect.

Connect to Exchange Online PowerShell

  1. On your local computer, open Windows PowerShell and run the following command.

$UserCredential = Get-Credential

In the Windows PowerShell Credential Request dialog box, type your account and password, and then click OK.

2. Run the following command.

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

3. Run the following command.

 

Import-PSSession $Session -DisableNameChecking

When you are done please be sure to disconnect the remote PowerShell session. If you close the Windows PowerShell window without disconnecting the session, you could use up all the remote PowerShell sessions available to you, and you’ll need to wait for the sessions to expire. To disconnect the remote PowerShell session, run the following command.

Remove-PSSession $Session

How do you know this worked?

After Step 3, the Exchange Online cmdlets are imported into your local Windows PowerShell session and tracked by a progress bar. If you don’t receive any errors, you connected successfully. A quick test is to run an Exchange Online cmdlet, for example, Get-Mailbox, and see the results.

Other interesting articles on Powershell and exchange management:

Get-EASDeviceReport.ps1 Script to Report on ActiveSync Devices

https://docs.microsoft.com/en-us/powershell/module/exchange/devices/get-activesyncdevice?view=exchange-ps

https://docs.microsoft.com/en-us/powershell/module/exchange/devices/get-activesyncdevicestatistics?view=exchange-ps

 

Michael