Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • 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 2012
    • 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

December 6, 2021 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 max.bak@woshub.onmicrosoft.com -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 max.bak@woshub.onmicrosoft.com -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 max.bak@woshub.onmicrosoft.com -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 max.bak@woshub.onmicrosoft.com }

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

http://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='j.sergo@woshub.com'}

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

Using PowerShell Behind a Proxy Server

July 1, 2022

Checking Windows Activation Status on Active Directory Computers

June 27, 2022

Configuring Multiple VLAN Interfaces on Windows

June 24, 2022

How to Disable or Enable USB Drives in...

June 24, 2022

FAQ: Licensing Microsoft Exchange Server 2019/2016

June 14, 2022

Leave a Comment Cancel Reply

Categories

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

Recent Posts

  • Using PowerShell Behind a Proxy Server

    July 1, 2022
  • How to Access VMFS Datastore from Linux, Windows, or ESXi?

    July 1, 2022
  • How to Deploy Windows 10 (11) with PXE Network Boot?

    June 27, 2022
  • Checking Windows Activation Status on Active Directory Computers

    June 27, 2022
  • Configuring Multiple VLAN Interfaces on Windows

    June 24, 2022
  • How to Disable or Enable USB Drives in Windows using Group Policy?

    June 24, 2022
  • Adding Domain Users to the Local Administrators Group in Windows

    June 23, 2022
  • Viewing a Remote User’s Desktop Session with Shadow Mode in Windows

    June 23, 2022
  • How to Create a Wi-Fi Hotspot on your Windows PC?

    June 23, 2022
  • Configuring SSH Public Key Authentication on Windows

    June 15, 2022

Follow us

woshub.com

ad

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • Whitelist Domains and Email Addresses on Exchange Server and Microsoft 365
  • How to Cleanup, Truncate or Move Log Files in Exchange Server 2013/2016/2019?
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Moving Exchange Mailboxes to Different Database
  • Export Exchange or Office 365 Global Address List (GAL) to CSV
  • How to Delete or Rename Default Mailbox Database in Exchange Server?
Footer Logo

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


Back To Top