Connect to Office 365 with PowerShell

Updated on 19 September 2016

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 two 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. But it will not hurt to install the Azure AD Module before restarting.

  1. Microsoft Online Services Sign-In Assistant for IT Professionals RTW
  2. Azure Active Directory Module for Windows PowerShell
    1. Reference
    2. Download; “AdministrationConfig-V1.1.166.0-GA.msi”

Once both tools are installed (and the machine has been 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 specific commands to be run locally…

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

Advertisements

One thought on “Connect to Office 365 with PowerShell

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s