Connecting to Office 365 / Exchange Online with PowerShell

This article shows how you can connect to Office 365 / Exchange Online using PowerShell.

References

Preparation of Windows PowerShell

The Windows PowerShell cmdlets for Office 365 management and deployment have to be installed on your local machine, if you want to manage e.g. Office 365 licences.

  1. Please download the appropriate version of the Microsoft Online Services Sign-in Assistant depending on your operating system version: http://technet.microsoft.com/en-us/library/hh974317.aspx.

  2. Install the downloaded software on your local machine.

Connect to Office 365 / Exchange Online


  1. Open the Windows PowerShell.

  2. Execute the follwoing command if the Microsoft Online Services Sign-in Assistant has been installed: Import-Module MSOnline

  3. Execute the following command $LiveCred = Get-Credential and enter the admin credentials in the popup dialog.

  4. Execute the following commands:
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    Import-PSSession $Session

  5. You are connected now. If the Microsoft Online Services Sign-in Assistant has been installed, execute the following command:
    Connect-MsolService –Credential $LiveCred

Enter credentials without popup dialog

You can enter the credentials directly in the script without using a popup dialog. This method may be used to automate PowerShell commands.

  1. Use the following commands instead of $LiveCred = Get-Credential and replace the credentials:
    $Username = "admin@yourdomain.edu"
    $Password = ConvertTo-SecureString ‘YourPassword’ -AsPlainText -Force
    $LiveCred = New-Object System.Management.Automation.PSCredential $Username, $Password