10 Useful PowerShell Scripts for Managing and Troubleshooting Microsoft Exchange Online

Today I'll share with you 10 of the most useful PowerShell scripts that we use for managing and troubleshooting Microsoft Exchange Online. PowerShell is an essential tool for Exchange administrators, enabling automation, streamlined management, and custom reporting capabilities. Here are the top scripts every Exchange Online admin should know.

Before beginning any of these commands against your Microsoft Tenant, you need to ensure that you have the ExchangeOnlineManagement module installed by doing the following within a PowerShell admin session:

PowerShell
Set-ExecutionPolicy RemoteSigned
Install-Module PowershellGet -Force
Install-Module -Name ExchangeOnlineManagement
  1. Connect to Exchange Online PowerShell

Now that you have the ExchangeOnlineManagement installed, you must first establish a connection. This script connects to Exchange Online PowerShell and imports the session.

PowerShell
Connect-ExchangeOnline -UserPrincipalName thedude@matrix7.com.au

Be sure to replace [email protected] with a user with Global Admin permissions from the example above. At this point you will be prompted to logon using your admin credentials for your Microsoft Tenant.

  1. Generate Mailbox Size Report

Monitoring mailbox sizes is crucial to managing storage efficiently. This script generates a report of all users' mailbox sizes in your organization.

PowerShell
Get-Mailbox -ResultSize Unlimited | ForEach-Object {
Get-MailboxStatistics -Identity $_.PrimarySmtpAddress |
Select DisplayName, ItemCount, TotalItemSize, LastLogonTime
} | Export-Csv -Path "C:\MailboxSizeReport.csv" -NoTypeInformation

This outputs a CSV file with mailbox names, item counts, size, and last logon time in the root of C:\ drive. Change the path in the example above to suit your requirements.

  1. List Inactive Mailboxes

Identifying inactive mailboxes is essential for licensing management. This script finds mailboxes that haven’t been accessed in the last 30 days. Change the value of 30 in the example below to suit your requirements.

PowerShell
$threshold = (Get-Date).AddDays(-30)
Get-Mailbox -ResultSize Unlimited | ForEach-Object {
$stats = Get-MailboxStatistics -Identity $_.PrimarySmtpAddress
if ($stats.LastLogonTime -lt $threshold) {
[PSCustomObject]@{
DisplayName = $_.DisplayName
LastLogonTime = $stats.LastLogonTime
}
}
} | Export-Csv -Path "C:\InactiveMailboxes.csv" -NoTypeInformation

4. Set Automatic Replies for a User

You might occasionally need to set automatic replies for users who are out of the office. This script sets up an automatic reply message for a specified user. Replace the user [email protected] in the example below to suit your requirements and the messages for both InternalMessage and ExternalMessage.

PowerShell
Set-MailboxAutoReplyConfiguration -Identity imonholidays@matrix7.com.au -AutoReplyState Enabled -InternalMessage "I'm currently out of the office bruh, catch ya when I get back." -ExternalMessage "I'm out of the office partying like it's 1999."
  1. List Shared Mailboxes

Shared mailboxes help teams collaborate, and this script lists all shared mailboxes in your Exchange Online environment.

PowerShell
Get-Mailbox -RecipientTypeDetails SharedMailbox | Select DisplayName, PrimarySmtpAddress

This provides a simple list of shared mailboxes along with their primary email addresses.

  1. Export Mailbox Permissions Report

Managing permissions effectively is crucial in Exchange Online. This script generates a report on permissions for each mailbox.

PowerShell
Get-Mailbox -ResultSize Unlimited | ForEach-Object {
Get-MailboxPermission -Identity $_.PrimarySmtpAddress |
Select Identity, User, AccessRights
} | Export-Csv -Path "C:\MailboxPermissions.csv" -NoTypeInformation
  1. Enable Litigation Hold for a User

Litigation hold is often required for compliance. This script enables litigation hold for a specified user and sets a hold duration in days. Change the user [email protected] and LitigationHoldDuration to suit your requirements.

PowerShell
Set-Mailbox -Identity litgationuser@matrix7.com.au -LitigationHoldEnabled $true -LitigationHoldDuration 365
  1. Monitor Email Forwarding Rules

Email forwarding can be a security concern. This script identifies any mailboxes with forwarding enabled.

PowerShell
Get-Mailbox -ResultSize Unlimited | Where-Object {$_.ForwardingAddress -ne $null -or $_.DeliverToMailboxAndForward -eq $true} |
Select DisplayName, ForwardingAddress, DeliverToMailboxAndForward
  1. Check Mailbox Quota Usage

Monitoring mailbox quotas helps avoid disruptions. This script identifies users close to their quota limit.

PowerShell
Get-mailbox | get-mailboxstatistics | select displayname,primarysmtpaddress,totalitemsize |sort totalitemsize -Descending | export-csv C:\MailboxSize.csv -NoTypeInformation
  1. Bulk Add or Remove Permissions to Multiple Mailboxes

Adding or removing access for multiple users at once is useful in large organizations. This script grants "Full Access" to a specific user for a list of mailboxes.

PowerShell
$Mailboxes = @("[email protected]", "[email protected]", "[email protected]")
$User = "[email protected]"

foreach ($Mailbox in $Mailboxes) {
Add-MailboxPermission -Identity $Mailbox -User $User -AccessRights FullAccess -InheritanceType All
}

To remove permissions, simply replace Add-MailboxPermission with Remove-MailboxPermission.

Conclusion

These PowerShell scripts are foundational for managing Exchange Online efficiently. By automating tasks, generating reports, and monitoring critical aspects of mailbox management, you can save time, increase accuracy, and focus on higher-value tasks. For further customisation, experiment with filters and advanced PowerShell scripting techniques to fine-tune these scripts to your environment’s specific needs.

If you've found this useful, you may want to sign up to our newsletter where you'll receive notices on when we post new articles and helpful "how tos". Just fill out your details below and we'll do the rest…

No Comments Yet.

Leave a comment


Sign up to our newsletter where you’ll receive notices on when we post new articles and helpful “how tos” to make your IT life easier.