To log into Office 365 using PowerShell, the Exchange Administrator will need to open PowerShell using elevalted permissions, aka as an Administrator. To do this in Windows 10 or Windows 11, start typing "powershell" on the start menu. When it comes up, choose Run as Administrator.
PowerShell V2
If this is the first time you are connecting to Exchange online using PowerShell, paste the following lines into PowerShell, one line at a time and press Y (or A) when prompted to enter Yes or No.
Set-ExecutionPolicy RemoteSigned winrm quickconfig Install-Module -Name AzureAD Install-Module -Name PowerShellGet -Force Install-Module -Name ExchangeOnlineManagement
Once the PowerShell module is installed use these commands to log in, then run your commands.
Import-Module ExchangeOnlineManagement Connect-ExchangeOnline -UserPrincipalName exchange-admin@address.com
Older Method
After opening PowerShell, type or paste:
$LiveCred = Get-Credential
Enter the administrator's username and password.
Type or paste this command:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Then this:
Import-PSSession $Session
Now you are ready to run the cmdlets.
When you are finished, you should end the session before closing the PowerShell window, otherwise your credentials are "live" for 15 minutes after the window is closed.
Remove-PSSession $Session
Using Microsoft Online Services Module for Windows PowerShell
In some cases the Online Services Assistant and the Online Services Module for Windows PowerShell files need to be used. If they are not already installed, links to the files are here.
Once installed, right click on the Microsoft Online Services Module for Windows PowerShell shortcut on the desktop and choose Run as Administrator.
Type (or paste) this line into the PowerShell window and logon using the administrator's username and password.
$LiveCred = Get-Credential
Next, type (or paste) this command
Connect-MSOLService -Credential $cred
It's now ready to enter the cmdlets.
Scripts are disabled error message
If you receive this error message: Import-PSSession : Files cannot be loaded because the running of scripts is disabled on this system. Please provide a valid certificate with which to sign the files.
Use this code then rerun the Import-PSSession $Session from above. If you did not receive the error, you are ready to use Powershell cmdlets.
Set-ExecutionPolicy Unrestricted
or
Set-ExecutionPolicy RemoteSigned
Video Tutorial
This video tutorial shows how to use the EXO PowerShell script to automate the log in process.
- Download Microsoft.PowerShell_profile.ps1 and remove .txt from the filename.
- Create a folder in your Documents folder called WindowsPowerShell.
- Move or Copy the PowerShell script into this folder.
- Start PowerShell using Run as Administrator.
- Type EXO and press Enter.
- Login and wait for the session to load.
- Type or paste the cmdlets you need to use.
More Information
Also see You can't run scripts in Microsoft Online Services Module for Windows PowerShell
Hi Diane,
I noticed you are using the older MSOnline PowerShell module in your examples. It may be useful to start using the newer Azure Active Directory PowerShell V2 module instead, as we will begin deprecating the MSOnline module when we have migrated the functionality of the MSOnline module to the newer module – currently planned for the Spring of 2017.
Thanks,
Rob de Jong