Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Connect and Manage Exchange Online with PowerShell

April 27, 2022 ExchangeMicrosoft 365PowerShell

How to Connect and Manage Exchange Online with PowerShell

PowerShell is a primary administration tool both for on-premises Exchange Server organizations and cloud-based Exchange Online in Microsoft 365. In this article, we’ll show how to install the Exchange Online PowerShell v2 (EXO V2) module on Windows, how to connect to your Exchange Online tenant, manage mailboxes and other Microsoft 365 objects.

Note that currently there are two modules to manage Exchange Online: Exchange Online PowerShell v1 and modern Exchange Online PowerShell v2 (EXO v2). Unless there is a clear reason to use an old module (legacy scripts, etc.), try to always use EXO v2: it works faster (like a new Azure AD module, it is based on the REST API in Microsoft Graph implementation), the cmdlets are optimized to handle multiple objects, Modern authentication is used instead of Basic one, MFA authentication is supported, ExchangeOnlineManagement 2.0.4 and newer versions work in PowerShell Core 6.x, 7x and on macOS, Ubuntu 18.04/20.04.

Contents:
  • Install Exchange Online PowerShell V2 (EXO V2) Module
  • Connect to Exchange Online with PowerShell
  • Managing Exchange Online Using ExchangeOnlineManagement Cmdlets

Install Exchange Online PowerShell V2 (EXO V2) Module

To install the Exchange Online PowerShell in Windows, you need PowerShell 5.x (PowerShell Core is supported in ExchangeOnlineManagement 2.0.4 or newer).

The PowerShell script execution policy settings on your computer must allow local *.PS files to run:

Set-ExecutionPolicy RemoteSigned

Install and update the PowershellGet module:

Install-Module PowershellGet -Force
Update-Module PowershellGet

To install the EXOv2 (ExchangeOnlineManagement) module from the PowerShell Script Gallery for all users, run this command:

Install-Module -Name ExchangeOnlineManagement -Force -Scope AllUsers

Then you can import the module into your session:

Import-Module ExchangeOnlineManagement

Make sure that the module has been installed. Its version is also displayed (it is 2.0.5 in my case):

Get-Module ExchangeOnlineManagement

install and import EchangeOnlineManagement (EXOv2) powershell module

In your scripts, you can make sure that the module is installed using the following command:

(Get-Module -ListAvailable -Name ExchangeOnlineManagement) -ne $null

To update the EXOv2 module, use the command below:

Update-Module ExchangeOnlineManagement

Connect to Exchange Online with PowerShell

To connect to your Microsoft 365 tenant using the Exchange Online module, the Connect-ExchangeOnline cmdlet is used. For example, you can specify the UPN of a user with a global admin role:

Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $true

Connect-ExchangeOnline PowerShell cmdlet with modern auth

Enter the user password and confirm sign-in using MFA (it is more convenient to use the Microsoft Authenticator app on your smartphone).

If no browser is installed on the computer (for example, when you connect to Exchange Online from Linux or Windows Server Core), you can display an authentication code for PowerShell Core 7.x and a box to enter it as follows:

Connect-ExchangeOnline -Device

Use the URL and code you have got on another computer with a browser.

Or use the –InlineCredential option to enter a password in your console interactively.

If your account has access to multiple Azure tenants, you can specify the tenant name using the DelegatedOrganization option:

Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $true -DelegatedOrganization woshubpreprod.onmicrosoft.com

If Modern Authentication is disabled for the account, you can connect as follows:

$cred = Get-Credential
Connect-ExchangeOnline -Credential $cred -UserPrincipalName [email protected] -ShowProgress $true

You can make sure if there is an active connection to Exchange Online as shown below:

Get-PSSession| ft –AutoSize

In our example, the connection to outlook.office365.com is active (State=Opened).

check that the Exchange Online PowerShell session is active

After you finish working with Exchange Online, exit your session correctly using the Disconnect-ExchangeOnline cmdlet. The matter is that Exchange Online supports only 5 concurrent PowerShell sessions. If you try to open a sixth one, the following error appears:

Fail to create a runspace because you have exceeded the maximum number of connections allowed.

You can use the following code in your PowerShell scripts to make sure that the connection to Exchange Online through the EXOv2 module is active. It will allow to avoid extra PowerShell sessions with Microsoft 365.

$PSSessions = Get-PSSession | Select-Object -Property State, Name
If (((@($PSSessions) -like '@{State=Opened; Name=ExchangeOnlineInternalSession*').Count -gt 0) -ne $true) {
Connect-ExchangeOnline -UserPrincipalName [email protected] }

The following article describes how to connect to on-prem Exchange or Exchange Online remotely via PowerShell without installing the EXOv2 module:

https://woshub.com/connect-exchange-microsoft-365-remote-powershell/

Managing Exchange Online Using ExchangeOnlineManagement Cmdlets

You can display a list of available cmdlets in the EXOv2 module using this command:

Get-Command -Module ExchangeOnlineManagement

Currently, 32 cmdlets are available:

  • Connect-ExchangeOnline
  • Connect-IPPSSession
  • Disconnect-ExchangeOnline
  • Get-WrappedCommand
  • IsCloudShellEnvironment
  • UpdateImplicitRemotingHandler
  • Get-EXOCasMailbox
  • Get-EXOMailbox
  • Get-EXOMailboxFolderPermission
  • Get-EXOMailboxFolderStatistics
  • Get-EXOMailboxPermission
  • Get-EXOMailboxStatistics
  • Get-EXOMobileDeviceStatistics
  • Get-EXORecipient
  • Get-EXORecipientPermission
  • Get-MyAnalyticsFeatureConfig
  • Get-OwnerlessGroupPolicy
  • Get-UserBriefingConfig
  • Get-VivaInsightsSettings
  • Set-MyAnalyticsFeatureConfig
  • Set-OwnerlessGroupPolicy
  • Set-UserBriefingConfig
  • Set-VivaInsightsSettings

EXO v2 ExchangeOnlineManagement cmdlets

Note that the names of some cmdlets in the Exchange Online module have changed. Most of them have the EXO suffix now. For example, use Get-EXOMailbox instead of Get-Mailbox, Get-EXOMailboxPermission instead of Get-MailboxPermission, etc. So if you have any scripts for EXOv1, you will need to carefully update them to use with EXOv2.

You can display a list of mailboxes in your Exchange tenant or information about a specific  mailbox:

Get-EXOMailbox |ft
Get-EXOMailbox jsergo

Get-EXOCasMailbox - get exchange online mailbox info

To show users with POP and IMAP access allowed:

Get-EXOCasMailbox -PropertySets Imap,pop

To display the size of all mailboxes:

Get-EXOMailbox | Get-EXOMailboxStatistics

Size of all shared mailboxes:

Get-EXOMailbox | Where-Object{$_.RecipientTypeDetails -eq "SharedMailbox"} | Get-EXOMailboxStatistics

Note that there is no Set-Mailbox cmdlet in the ExchangeOnlineManagement module. The matter is that other available Exchange Online cmdlets (over 750) are imported into your PowerShell session after connecting to a tenant. First of all, you must get a session name:

Get-PSSession| select ConfigurationName,CurrentModuleName

Using the generated session name, you can get a list of available cmdlets:

Get-Command -Module tmp_4amp2awk.dga

get imported cmdlets list from exchange online

To change the user’s SMTP address, you can use the command below:

Get-EXOMailbox jsergo | Set-Mailbox -EmailAddresses @{Add='[email protected]'}

0 comment
2
Facebook Twitter Google + Pinterest
previous post
Using WinGet Package Manager on Windows 10 and 11
next post
Disable Outlook Mailbox Auto-Mapping in Exchange/Microsoft 365

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016
  • FAQ: Licensing Microsoft Exchange Server 2019/2016
  • Whitelist Domains and Email Addresses on Exchange Server and Microsoft 365
  • Moving Exchange Mailboxes to Different Database
  • How to Cleanup, Truncate or Move Log Files in Exchange Server 2013/2016/2019?
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top