Home » Office 365 » Connect to Exchange Online Using PowerShell

Connect to Exchange Online Using PowerShell

Microsoft Exchange Online is a critical component of Microsoft Office 365, providing email and calendaring services. It allows business to securely access their emails, calendars, and contacts from anywhere.

Managing Exchange Online is made efficient and flexible with PowerShell, enabling IT administrators to perform tasks and automate processes.

To connect to Exchange Online using PowerShell, follow the below steps:

  1. Install Module ExchangeOnlineManagement.
  2. Import the module
  3. Connect to Exchange Online using the Connect-ExchangeOnline cmdlet.
  4. Use cmdlets that are available in the Exchange Online module to manage Exchange Online objects such as emails, contacts, and calendars.

In this article, we will discuss how to connect to Exchange Online using PowerShell.

How to Install Exchange Online PowerShell Module

To install the Exchange Online PowerShell module, follow these steps.

Step 1: Open the Windows PowerShell terminal ( Run as Administrator)

Step 2: Install the ExchangeOnlineManagement module using the following command.

Install-Module -Name ExchangeOnlineManagement

Click Yes or type Y to install the module from an untrusted repository.

Install ExchangeOnlineManagement module in PowerShell
Install the ExchangeOnlineManagement module in PowerShell

How to Connect to Exchange Online with PowerShell

To connect to Exchange Online with PowerShell, import the module “ExchangeOnlineManagement” in the current PowerShell session and use the Connect-ExchangeOnline cmdlet.

import-module ExchangeOnlineManagement

The Connect-ExchangeOnline cmdlet connects to your Microsoft Office 365 Exchange Online.

Connect-ExchangeOnline

It will prompt Sign in to your account dialog box.

You can use the credentials (work or school account name and password) or multi-factor authentication to connect with Exchange Online.

To connect to the Exchange Online user account, run the following command.

Connect-ExchangeOnline -UserPrincipalName [email protected]  

In the above PowerShell script,t the Connect-ExchangeOnline command uses the UserPrincipalName parameter to specify the user principal name “[email protected]” and connect to the Exchange Online.

How to Verify You Are Connected to Exchange Online

To verify that you are connected to Exchange Online, use the PowerShell cmdlet Get-AcceptedDomain.

Get-AcceptedDomain

If you don’t receive any errors, it means you have connected successfully. This command will return the following results when connected successfully.

PS C:\WINDOWS\system32> Get-AcceptedDomain

Name                           DomainName                     DomainType                                                      Default
----                           ----------                     ----------                                                      -------
shellgeeklab.onmicrosoft.com   shellgeeklab.onmicrosoft.com   Authoritative                                                   True

How to Get Cmdlets Available in Exchange Online Module

To get a list of all available cmdlets in Exchange Online, run the following command.

Get-command -Module ExchangeOnlineManagement

This command returns a list of cmdlets in the ExchangeOnlineManagement module.

Get all Exchange Online cmdlets

How to Get All MailBoxes in Exchange Online

To get all mailboxes in Exchange Online, run the following command.

Get-EXOMailbox

In the above PowerShell script, the Get-ExoMailbox command returns a list of all mailboxes in the Exchange Online.

Connect to Exchange Online using PSSession

To connect to Exchange Online using a remote PowerShell session, use the following steps.

  1. Get the credentials to connect to the Exchange Online.
  2. Create a New PS session to connect to Microsoft Office 365.
  3. Import the Exchange Online PS session.
  4. Run the Get-MailBox cmdlet to get a list of mailboxes.
  5. Remove the current PowerShell session.
# Get the credentials
$Cred = Get-Credential

# Create a Exchange Online PowerShell session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection

# Import the PowerShell session
Import-PSSession $Session -DisableNameChecking

#Get the mail box
Get-Mailbox
  
# Remove the session
Remove-PSSession $Session

How to Disconnect the Exchange Online

To disconnect the Exchange Online PowerShell session, use the Disconnect-ExchangeOnline cmdlet.

Disconnect-ExchangeOnline -Confirm:$False

This command will silently disconnect the Exchange Online session without a confirmation prompt.

Conclusion

I hope the above article on how to connect to Exchange Online with PowerShell is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Articles

Connect-MsolService