Connect to Office 365 Exchange PowerShell

KB ID 0001410

Problem

If there’s one thing thats grown on me it’s PowerShell, After the last few versions of Exchange you can’t really escape it. So now we have so many clients with their Exchange in Office 365. The ability to connect to that, and use all your usual Exchange commandlets is a bonus!

WARNING: This process may end up with you getting an “Access is denied” error, if you are using modern authenticiction, or MFA. If so use this article instead.

Solution

If you haven’t already done so, you need to ‘slacken‘ your signing policy, (a little) before proceeding;

[box]Set-ExecutionPolicy RemoteSigned[/box]

Now to access Exchange online you need to be able to authenticate to it, the best way to do that is to ‘cache’ your logon credentials. (Unless you have ADFS Federation then you can skip this step). To enter your O365 creds execute the following command;

[box]$UserCredential = Get-Credential[/box]

Then create the settings for your remote session;

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

Then to open the session;

[box]Import-PSSession $Session[/box]

DON’T FORGET: When you are finished, to disconnect the session with the following command;

[box]Remove-PSSession $Session[/box]

Related Articles, References, Credits, or External Links

NA