How to connect and remotely manage Office 365 with PowerShell

Office 365 web interface was designed to make it easier to use right down to its administrative bowels. On the one hand it really is quick and simple to navigate, on the other it definitely lacks some advanced configuration options so loved by sysadmins.

Luckily there is the mighty PowerShell coming to the rescue! You should already know its potential, which can also be utilized in Office 365. Below you’ll find out how.

System Requirements

To remotely manage Office 365 with PowerShell your machine must meet the following requirements:

Once you have all the components installed you are ready to connect to Office 365.

Connecting to Office 365 with PowerShell

First you need to import all the relevant cmdlets. To do so in PowerShell command window run

Import-Module MSOnline

Next run the code below and enter your logon credentials (your Office 365 administrative user email and password).

$365Logon = get-credential

Now configure your remote PowerShell session variables.

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

Run the code below to connect.

Import-PSSession $365PSSession -AllowClobber

If you encounter any runtime errors at this point, it is most probably because of the disabled script execution policy. You can enable it running the command below:

Set-ExecutionPolicy RemoteSigned

The final step is to connect to your Office 365 online service

Connect-MsolService –Credential $365Logon

That’s it! You are now ready to run PowerShell commands.

Below you can find the whole process in one script, which you can save to the PS1 file.

Import-Module MSOnline
$365Logon = get-credential
$365PSSession = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $365Logon -Authentication Basic -AllowRedirection
Import-PSSession $365PSSession -AllowClobber
Set-ExecutionPolicy RemoteSigned
Connect-MsolService –Credential $365Logon

Whenever you need to remotely administer your Office 365 with PowerShell simply run the PS1 script with the code above. Alternatively, you can add it to your PowerShell profile so you are ready to rock automatically right after launching PS or PowerShell ISE.

To learn about available Exchange Online cmdlets see this TechNet article.

Delegate selected Office 365 management duties to non-IT staff

Let your Marketing team manage Office 365 email signatures (CodeTwo Email Signatures)
Full control over Out of Office settings in Office 365 (CodeTwo Out of Office Manager)

How to connect and remotely manage Office 365 with PowerShell by

One thought on “How to connect and remotely manage Office 365 with PowerShell


Leave a Reply

Your email address will not be published.

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>