Connect to Exchange Online From PowerShell

On this post I’m going to show you how to connect to Exchange Online from PowerShell and a couple of basic commands.

As an Exchange Administrator one of my daily tasks is open a remote session from my computer to Exchange Online and manage our mailboxes or set any configuration or retrieve information.

Usually in order to connect to Exchange Online, you need export your credentials to a local variable in PowerShell and then add that variable as parameter of the command to connect to Exchange Online, Example:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
If you entered a valid credentials you are going to get a result similar to this image.

Once you entered the last command, you will be connected to Exchange Online and then you can manage your mailbox hosted in Office 365.


I’m to lazy to enter my credentials all the time and for that reason I’m using an encrypted file with my credentials to connect to Exchange Online.

But how to do that, easily, from PowerShell too.

Get-Credential | Export-Clixml ".\SecuredCredentials.xml"

Running the above command, we are going to generate a XML file with our credentials.

The cool things:

  • The password is encrypted
  • The generated file only works with the user and computer where was generated.
  • If another user stolen this file, he/she can’t use the file on another machine.
  • You can generate this file on another server if you want to Schedule Scripts to ran without need ask for your password.

So the next time that you want to connect to Exchange Online you can use the next line in order to import the credentials stored on your file:

$UserCredentials = Import-Clixml ".\SecuredCredentials.xml"


  • You can have up to 3 PowerShell sessions per user in Exchange Online.
  • After finish with your work on the session it’s a good idea to terminate the session running the next command: Get-PsSession | Remove-PsSession

Let me know if you have any comments.


Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: