As powerful as Office 365 is, it can be made even more powerful with simple PowerShell cmdlets that can streamline your work and make your life easier. In today’s “Deep Dive,” we take an extended look into using PowerShell to collect mailbox information from Exchange Online.
In the previous part of this article series, we gave you an overview of Get-MailBox cmdlet and a few examples to get you started using the cmdlet to collect mailbox information from Office 365 Exchange Online. In this part and subsequent parts, we will look at some useful parameters of the Get-MailBox cmdlet and how you can use these parameters to get the desired information without spending much time with scripting.
As we mentioned earlier, Get-MailBox cmdlet is available in both the on-premises Exchange Server installation and Exchange Online service. However, some parameters of Get-Mailbox cmdlet are not available to one environment or the other. For example, parameter ReadFromDomainController is only available in on-premises Exchange Server installation. Similarly, IncludeInactiveMailbox is also only available on-premises.
“Identity” is the most common parameter that you will be using with Get-MailBox cmdlet. By using this parameter, you are instructing Get-MailBox to look for a mailbox that matches the given identity. For example, you can use “Email Address” with “-Identity” parameter to get the mailbox information of a user whose email address matches the email address you specified in the “-Identity” parameter. For example, to get mailbox information for johndthomas@TechGenix.com, you will execute the below PowerShell command:
Get-MailBox –Identity JohnDThomas@TechGenix.com
This command gets mailbox information by querying Exchange Online for an email address that matches with the given email address. Get-MailBox cmdlet supports other values to be used with the “-Identity” parameter as explained in the table.
|Name||Specify the mailbox name of the user.|
|Display Name||Need to specify the display name of the Office 365 user.|
|Alias||Use Mailbox alias. In most cases, Alias value is similar to name value.|
|Distinguished Name||Provide the distinguished name of the user.|
|Email Address||As explained in an example earlier, use the email address of the user.|
|GUID||Specify the GUID of the Office 365 user.|
|SamAccountName||SamAccountName of the user.|
|Office 365 User ID||As the name suggests, specify the Office 365 user ID for which you want to retrieve the mailbox information.|
|User Principal Name or UPN||This is the UPN of the Office 365 user.|
While you can use the above PowerShell command to retrieve the mailbox for an individual identity, if you need to retrieve mailbox information for multiple users, you need to use ForEach Loop in a PowerShell script. Let’s say you need to collect mailbox information for 200 email addresses. While you can use the above PowerShell command to retrieve mailbox information for each user one by one, it is going to take a considerable amount of time if you need to perform the task for 200 users manually. What you can do is create a text file, add all email addresses to the CSV file, and then use the below PowerShell script to collect required information. Note that when you run the Get-Mailbox command, it will show the default mailbox information, such as name, alias, server name where the mailbox is hosted, and other information. You might not want to see all information if you are looking for something specific. For example, if you need to know the “Alias” name for 200 mailboxes, all you need to do is use an “IF” condition in the PowerShell script as shown in the script below:
$MailBoxAliasReport = "C:\Temp\MailBoxAliasReport.CSV"
Remove-item $MailBoxAliasReport -ErrorAction SilentlyContinue
$STR = "Email Address, Name, Current Alias"
Add-Content $MailBoxAliasReport $STR
$EmailAddressFile = "C:\Temp\EMailAddresses.txt"
$GCEmailAddresses = GC $EmailAddressFile
ForEach ($Item in $EmailAddressFile)
$ThisEmailAdd = $Item
Write-Host "Processing Email Address : " $ThisEmailAdd
$MailBoxInfo = Get-MailBox -Identity $ThisEmailAdd | Select-Object Name, Alias
$STRNew = $ThisEmailAdd+","+$MailBoxInfo.Name+","+$MailBoxInfo.Alias
Add-Content $MailBoxAliasReport $STRNew
Write-Host "All Email Addresses from file" $EmailAddressFile " file were checked."
Write-Host "Report is generated in $MailBoxAliasReport file."
Once you have finished executing the above PowerShell command, a CSV report will be generated under “C:\Temp\MailBoxAliasReport.CSV” file, which contains the required information as shown in the screenshot below.
As you can see, the PowerShell script collected required information only for the email addresses that were specified in the C:\Temp\EmailAddresses.txt file. The ForEach loop command in the above PowerShell script hits only the required email address, collects the current alias of the mailbox user, and then stores the value in the report file.
In case you would like to retrieve other mailbox information, such as display name, server name, and mailbox size, you need to know the required attribute. You can easily get the supported attributes by using the Get-Member cmdlet.
This article provided an overview of the “Identity” parameter of the Get-MailBox cmdlet. We also provided a PowerShell script that you can use to collect mailbox information for multiple Office 365 users. Coming up, we will continue to explain the parameters supported by the Get-Mailbox cmdlet and also explain the attributes supported by the Get-MailBox cmdlet.
If you would like to read the other parts in this article series, here are the links:
If you want your business to succeed these days, you need a user-friendly website. Put these tools in your toolbox…
Do businesses rely too heavily on email for communication? Yes! Is the answer to replace email with chat apps? No!…
If you got your start with Visual Basic, you remember msgbox. This PowerShell function will bring back memories and help…
Microsoft Ignite 2019 included a slew of updates on products and services. Among the announcements was Azure Arc, which may…
Choosing communication tools is like choosing a first progamming language. While you want easy, you also want cross-platform, security, and…