Skip to content
All quick references

Exchange PowerShell

23 snippets · 7 sections

Copy-ready Exchange PowerShell commands for connecting to Exchange Online or on-premises Exchange, checking mailbox and database health, and investigating mail flow.

01

Connect to Exchange Online

Install the Exchange Online module

Install-Module ExchangeOnlineManagement -Scope CurrentUser
Import-Module ExchangeOnlineManagement

Connect with an interactive sign-in

Connect-ExchangeOnline -UserPrincipalName admin@contoso.com

Verify the Exchange Online connection

Get-ConnectionInformation |
  Format-Table UserPrincipalName, ConnectionUri, State, TokenStatus

Connect using a certificate-based app

Connect-ExchangeOnline `
  -AppId $AppId `
  -Organization contoso.onmicrosoft.com `
  -CertificateThumbprint $CertificateThumbprint

Disconnect from Exchange Online

Disconnect-ExchangeOnline -Confirm:$false
02

Exchange Online Essentials

Find cloud mailboxes

Get-EXOMailbox -ResultSize 10 |
  Select-Object DisplayName, UserPrincipalName, PrimarySmtpAddress, RecipientTypeDetails |
  Format-Table -AutoSize

Check cloud mailbox statistics

Get-EXOMailboxStatistics -Identity jane.doe@contoso.com |
  Select-Object DisplayName, ItemCount, TotalItemSize, LastLogonTime |
  Format-List

Search message traces

Get-MessageTraceV2 `
  -StartDate (Get-Date).AddHours(-2) `
  -EndDate (Get-Date) `
  -RecipientAddress user@contoso.com |
  Select-Object Received, SenderAddress, RecipientAddress, Subject, Status |
  Format-Table -AutoSize
03

Connect to On-Premises Exchange

Create a remote Exchange session

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
  -ConnectionUri http://FQDN/PowerShell/ `
  -Authentication Kerberos `
  -Credential $UserCredential
Import-PSSession $Session -DisableNameChecking

Close the session

Remove-PSSession $Session

Confirm imported Exchange commands

Get-Command Get-MailboxDatabase
Get-PSSession | Select-Object ComputerName, State, ConfigurationName
04

Database Health

List mounted mailbox databases

Get-MailboxDatabase -Status |
  Sort Name |
  Format-Table Name, Server, Mounted

Inspect database copy health

Get-MailboxDatabaseCopyStatus * |
  Format-Table Name, Status, CopyQueueLength, ReplayQueueLength, ContentIndexState

Find mailbox database statistics

Get-MailboxDatabase -Status |
  Select-Object Name, DatabaseSize, AvailableNewMailboxSpace, LastFullBackup |
  Format-Table -AutoSize
05

Mail Flow and Queues

Review transport queues

Get-Queue

Show messages in a queue

Get-Queue -Server EX01 |
  Get-Message |
  Format-Table Identity, Subject, FromAddress, Status, LastError

Test mail flow

Test-Mailflow -TargetEmailAddress administrator@contoso.com
06

Mailbox Administration

Find a mailbox

Get-Mailbox -Identity jdoe |
  Format-List DisplayName, PrimarySmtpAddress, Database, RecipientTypeDetails

List mailbox sizes

Get-MailboxStatistics -Database DB01 |
  Sort-Object TotalItemSize -Descending |
  Select-Object DisplayName, ItemCount, TotalItemSize, LastLogonTime |
  Format-Table -AutoSize

Move a mailbox

New-MoveRequest -Identity jdoe -TargetDatabase DB02

Get-MoveRequest -Identity jdoe |
  Get-MoveRequestStatistics |
  Select-Object DisplayName, Status, PercentComplete, BytesTransferred
07

Troubleshooting

Search message tracking logs

Get-MessageTrackingLog -Start (Get-Date).AddHours(-2) `
  -Recipients user@contoso.com |
  Select-Object Timestamp, EventId, Source, Sender, Recipients, MessageSubject |
  Format-Table -AutoSize

Check Exchange services

Test-ServiceHealth |
  Format-Table Role, RequiredServicesRunning, ServicesNotRunning

Check a receive connector

Get-ReceiveConnector -Server EX01 |
  Select-Object Name, Enabled, Bindings, RemoteIPRanges |
  Format-List