How to Connect to Office 365 Using Powershell

This article shows you how to connect to Office 365 using Powershell.

Powershell allows you to make changes to your Office 365 account and users from the command line.

1. Make sure you are an Office 365 administrator.

2. Run Powershell as an administrator.

3. If you are connecting for the 1st time you need to download the Microsoft Online Services Sign-In Assistant for IT Professionals RTW which installs Microsoft Online Services Sign-In Assistant which provides end user sign-in capabilities to Microsoft Online Services, such as Office 365.

4. So you don't get the 'Untrusted repository' warning first run the command

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

MSOnline module for managing Azure Active Directory (users and security groups)

1. Install the Microsoft Azure Active Directory Module for Windows PowerShell using the command

Install-Module MSOnline

2. Connect to the Azure AD for Office 365 using your account login by running the command

Connect-MsolService
. The will be a pop up for you to sign in with.

3. Check you are connected correctly by using the command to get all user accounts

Get-MsolUser

Implicit Remoting for Exchange Online

1. Connect to Exchange server via PSRemoting

$Credential = Get-Credential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $ExchangeSession

At the end of your session disconnect by using

Disconnect-PSSession -Name $ExchangeSession

Setting the PowerShell Execution Policy

You might get the error Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files. when trying to run scripts.

You can view the current policy by using the command

get-executionpolicy

To allow scripts to be RemoteSigned then run the command

Set-ExecutionPolicy RemoteSigned

Return to Category