Manage Learn to apply best practices and optimize your operations.

Use Exchange Online PowerShell scripts to blaze through routine tasks

Administrators tasked with handling many Exchange Online mailboxes can perform tasks, such as setting mailbox size limits and searching for messages, in bulk with PowerShell.

Managing an on-premises Exchange Server can be a complex task, particularly when the number of mailboxes rises...

quickly. Automation with PowerShell can help get a lot of work done without spending much time managing objects one at a time. Administrators whose organizations plan to host their email in Office 365 won't leave all their automation skills behind when they move to the cloud. These Exchange Online PowerShell scripts will run bulk operations to create and administer multiple mailboxes at once.

Make the connection

Since Exchange Online is an online service, the first step to managing users is to make that initial connection. Download and install the Microsoft Online Sign-in Assistant, and then download and install the Azure Active Directory PowerShell Module. Use the script below to connect a remote PowerShell session to the Exchange Online service and then import the required cmdlets.

$credential = Get-Credential

$session = New-PSSession -ConfigurationName Microsoft.Exchange `

                         -ConnectionUri https://outlook.office365.com/powershell-liveid/ `

                         -Credential $credential `

                         -Authentication Basic `

                         -AllowRedirection

Import-PSSession $session -DisableNameChecking -AllowClobber

Connect-MsolService -Credential $credential

The Exchange team used some unapproved verbs in the module. We use the -DisableNameChecking parameter to suppress the warning that will get displayed otherwise.

Managing Office 365 mailboxes

One of the most basic tasks is to find the Office 365 mailboxes and get their information. Run the Exchange Online PowerShell script in Figure 1 to find a mailbox and get its usage statistics.

Find Office 365 mailbox.
Figure 1: Use an Exchange Online PowerShell script to find a mailbox in Office 365.

The script doesn't return much information in Figure 1, but it's just an abbreviated listing. Adding | Get-Member to the end of the script returns the full list of properties from the Get-MailboxStatistics cmdlet. With those properties, administrators can generate the information shown in Figure 2.

Office 365 mailbox statistics
Figure 2: Use the Get-MailboxStatistics cmdlet to display mailbox statistics.

Use an Exchange Online PowerShell script to create new Office 365 mailboxes as well. Assume there is a comma-separated values text file with a list of users, as shown in Figure 3.

CSV file
Figure 3: Using a comma-separated values file to create new mailboxes in Office 365.

With the text file in place, the script in Figure 4 can create the users and their mailboxes.

Mailbox script
Figure 4: A script calls the comma-separated values file and builds the new Office 365 mailboxes.

The $newBoxes variable contains the new mailboxes created by the script. Administrators can call that variable to return the new boxes as shown in Figure 5.

Mailbox variable
Figure 5: The $newBoxes variable shows the information associated with the new mailboxes.

As the script creates the users and their corresponding mailboxes, it will likely return an error asking the administrator to issue a license to the users. Fix that with this script:

Get-MSOLUser | Where-object userPrincipalName -in $newBoxes.userPrincipalName | Set-MsolUserLicense -AddLicenses <tenant>:ENTERPRISEPACK

Use the script below to verify owner rights to check what's getting sent and received.

$newBoxes | Foreach-Object {Add-MailboxFolderPermission -Identity $_.UserPrincipalName -User <myusername@domain> -AccessRights Owner}

Since these are test mailboxes, consider lowering the size limit to 5 gigabytes, as shown in Figure 6.

Set mailbox quota
Figure 6: Use PowerShell to set a 5 gigabyte limit on the Exchange Online mailbox.

Exchange Online PowerShell scripts for searching

An Office 365 administrator needs to perform a mailbox search occasionally. The Exchange Online PowerShell script below can search through all the mailboxes and gather the results into one place for review.

Get-Mailbox | Search-Mailbox -SearchQuery 'Subject:"Microsoft Office"' -TargetMailbox "Discovery Search Mailbox" -TargetFolder "MSOffice Mail" -LogLevel Full

Connect to the mailbox Discovery Search Mailbox in Outlook to see the results in a discovery mailbox. The Discovery Search Mailbox should have a new folder called MSOffice Mail that contains the search results. If the query finds a match, it puts a copy of the message to that mailbox folder; the messages can be viewed in Outlook like any other mailbox.

While this query is simple, administrators can build complex ones using this Keyword Query Language syntax reference guide.

Deactivating Office 365 mailboxes

Disabling a mailbox is easy, but this procedure requires caution. An inexperienced Exchange administrator may not know that disabling a mailbox detaches it from its Active Directory object. When you are prepared to proceed, use this script:

Get-Mailbox -filter 'Name -like "test*"' | Disable-Mailbox -Archive

Next Steps

Generate mailbox reports with Exchange Online scripts

The Search-Mailbox cmdlet helps track down key messages

Key management techniques for Office 365 administrators

This was last published in November 2016

PRO+

Content

Find more PRO+ content and other member only offers, here.

Essential Guide

The essential admin's guide to Office 365 PowerShell

Join the conversation

2 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What are some methods or tasks you use to manage Exchange Online with PowerShell?
Cancel

Is there a way to connect to exchange online using MFA I have the most current AzureAD 2.0.0.33.

Cancel

-ADS BY GOOGLE

SearchWindowsServer

SearchEnterpriseDesktop

SearchCloudComputing

SearchSQLServer

Close