Office 365–Script to connect to Office 365 Exchange via PowerShell

2017-12-08T13:27:19+00:00 July 19th, 2011|Exchange, Office 365, PowerShell|

I am currently doing some testing around the new Office 365 suite with my own test beta account. I have just verified my test domain and was now looking to changing some of the Exchange setting in Office 365 but I could not find the way to do it using the online GUI.

After a bit of research I realised that I can connect to my Office 365 Exchange environment via PowerShell and run commands as you would with an on premise Exchange 2010 server.

I have created a script for this which will prompt you for your Office 365 credentials, simply copy the lines below and paste them in notepad and save the file as whatever you like just make sure it has a .ps1 extension at the end, I have called mine office365.ps1:

 

$cred = Get-Credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
$importresults = Import-PSSession $s

 

NOTE: You may need to run the following command in your PowerShell in order for the Import-PSSession command to work correctly: Set-ExecutionPolicy RemoteSigned

 

Once the file has been created, open up a PowerShell window and run as administrator. To run the command, change directory into where you saved the file and then run it (.office365.ps1 and press enter). The script should then prompt you for your Office 365 credentials:

image

Put in your Office 365 username as user@domain.onmicrosoft.com and then put in your password and click on OK.

As the script runs you will get a warning message like the following that can be ignored:

WARNING: Your connection has been redirected to the following URI:
"
https://pod51013psh.outlook.com/PowerShell-LiveID?PSVersion=2.0 "

If the script runs successfully then you should return to the PowerShell prompt. A good test to see if this has worked is to run the Get-Mailbox command over one of your users in the Office 365 environment to see if you can get information about their mailbox:

image

This will give you a limited amount of Exchange PowerShell command line functions as compared with an on premise Exchange 2010 environment but it will give you all the basics required to make advanced changes to your Office 365.

Hope this helps.

Paul