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.
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
(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
Enter the user password and confirm sign-in using MFA (it is more convenient to use the Microsoft Authenticator app on your smartphone).
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
).
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] }
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
You can display a list of mailboxes in your Exchange tenant or information about a specific mailbox:
Get-EXOMailbox |ft
Get-EXOMailbox jsergo
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
To change the user’s SMTP address, you can use the command below:
Get-EXOMailbox jsergo | Set-Mailbox -EmailAddresses @{Add='[email protected]'}