Wednesday, May 8, 2013

How to Connect to Office 365 with Windows PowerShell





Windows PowerShell is a powerful tool for managing users in Office 365. We've looked at setting up PowerShell to work with Office 365. Let's look at how to connect PowerShell to Office 365.

If you haven't already setup Windows PowerShell to work with Office 365 please see: How to Install and Configure PowerShell for Office 365 in Windows 8. The procedure's practically the same for Windows 7.

Run the Windows Azure Active Directory Module for Windows PowerShell as administrator


To run the scripts needed to connect to Office 365 we must first set the script execution policy. At the PowerShell prompt with the following cmdlet (PowerShell command):

Get-ExecutionPolicy


The execution policy needs to be RemoteSigned. RemoteSigned allows only downloaded scripts signed by a trusted publisher to be run. If Get-ExecutionPolicy doesn't return RemoteSigned, it must be changed by entering the following cmdlet and entering a "y" when prompted:

Set-ExecutionPolicy RemoteSigned


Now Get-ExecutionPolicy returns RemoteSigned


Next we need to provide Office 365 with administrative credentials to be able to make changes. We're going to assign our credentials to the variable $MSCred...

$MSCred = Get-Credential


...and PowerShell prompts for an Office 365 account with administrative permissions.


After entering the credentials of a Office 365 administrator account and clicking ok, we need to open a connection to the Office 365 servers. The following command opens a connection, passes on our credentials, and assigns the variable $Session to our session.

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $MSCred -Authentication Basic -AllowRedirection


Once the connection is open we need to import the commands for the Exchange Server shell.

Import-PSSession $Session


At this point we are ready to issue commands to Office 365.

When done making changes on Office 365 close the session. If you do not close the session, it will remain available until it times out.

Remove-PSSession $Session


No comments: