How to Remote PowerShell into Exchange Online using Prefix



In my last blog I wrote about how to connect to Exchange Online using a remote PowerShell session.

But if your are running your on-premise Exchange 2010 and connecting to Exchange Online at the same time, it would be nice to be able to distinguish between the commands you run in PowerShell against the on-premise or the online Exchange.

This can be done using a prefix setting, when you import your Exchange Online session.

See my last blog post for the details about connecting to Exchange Online: How to Remote PowerShell into Exchange Online (Office 365).

Here’s a quick walk though the initial steps needed, before we get to the part, that is different from my last blog post about the subject. For more details, see my last blog post.

Run the following commands:

To connect to Exchange Online, just start a PowerShell session and type in following commands:

$Cred = Get-Credential


The above command prompts you, for your Office 365 tenant admin credentials, it is important to enter as UPN format, such as e.g.

Next, we will create a new remote PowerShell session using the following cmdlet:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $cred -Authentication Basic –AllowRedirection


The part below is new from my last blog post and where we add the prefix to our command, during the import of the PowerShell session:

Import-PSSession $s –prefix o365


You are now connected to Exchange Online with you Office 365 Tenant admin and can use Exchange Online PowerShell cmdlets available.

Now when you run Exchange commands in PowerShell, you have to remember to prefix them with o365, otherwise you run commands against your on-premise Exchange (if connected from where you run this).

An example could be getting a list of mailboxes as the above screenshot, the command would be:



For information about Exchange Online PowerShell cmdlets available see Reference to Available PowerShell Cmdlets in Exchange Online.

To remove the PowerShell session again, run the following command:

Remove-PSSession $s

Comments are closed.