Connect to Office 365 with PowerShell

Updated on 11 January 2019

To connect to and manage Office 365 with the Azure AD Module for PowerShell, there are two prerequisites needed.

NOTE: If planning to run the tasks from an Exchange server, the MSOL commands (i.e. Connect-MsolService) may not work. If this is the case, install the tools on and run the commands from an admin workstation.

NOTE: Make sure you select the proper version of each tool for the machine they are being installed on. As of 20 October 2014, only the 64-bit versions are available.

NOTE: A restart of the machine may be required after installing the Sign-In Assistant.

 

Once the Sign-In Assistant is installed (the machine may need to be restarted), open an elevated Active Directory Module for Windows PowerShell console (not Windows PowerShell) and enter the following commands to connect to Office 365. This first command may be required if you haven’t already set the execution policy level on the machine we are using to connect to O365…

Set-ExecutionPolicy Unrestricted -Force

 

This command will pop up the window required for you to enter your Office 365 credentials. In the login pop-up, enter Office 365 global admin credentials to proceed. The user name must be entered as a fully qualified name (i.e. admin@domain.onmicrosoft.com).

$O365CREDS = Get-Credential
Next, enter this command to create a new cloud-based PowerShell session using the cached credentials…
$SESSION = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365CREDS -Authentication Basic -AllowRedirection

 

This command brings that cloud-based PowerShell session to the local computer…

Import-PSSession $SESSION

 

Lastly, we will download the O365 commands specific for user management…

Install-Module MSOnline

 

To install the NuGet provide and MSOnline module, we have to enter “Y” twice.  And then we can run the following to manage users in O365 via PowerShell…

Connect-MsolService -Credential $O365CREDS

 

You should now be able to do pretty much anything you want to your tenant via PowerShell.

Good luck.

 

Reference(s):


Originally published on 21 March 2013 at http://oddytee.blogspot.com/2013/03/connect-to-office-365-with-powershell.html

2 thoughts on “Connect to Office 365 with PowerShell

  1. Pingback: Remove Users, Contacts and Groups from Office 365 | Cloud Evangelist

  2. Pingback: Synchronize a new Active Directory domain with an existing Office 365 tenant – My WordPress

Leave a comment