Last Update: Nov 19, 2024 | Published: Feb 14, 2012
Office 365 provides options for working with Exchange online, SharePoint online, and Office Web Apps, in addition to features such as Active Directory Sync and access to full featured Office 2010 Professional software.
While using Office 365 in either a production environment or a 30 day trial, you have the ability to use PowerShell for command line access to account administration. This article describes one scenario that you will want to perform: Creating a usage report to determine the sizes of each users’ mailbox.
If you haven’t yet setup an account for Office 365, but would like to follow these exercises, it’s very easy to setup a trial account. Follow the instructions to Setup an Office 365 Trial and Configure it for Your Own Domain Name.
If you’ve already got an account, either a trial or subscription, then you’re ready to configure PowerShell to connect to your Exchange Online account.
If you don’t have PowerShell 2.0, go download the Windows Management Framework and install it. Remote sessions are needed, so you’ll need PowerShell 2.0. After you’ve got that, you’ll need to start an administrative console. My administrative sessions are always colored red, so I know they’re ready to make changes to the system.
Next you’ll need to make a few configuration changes.
Set a few configuration items:
Now you’re ready to make a PowerShell connection to Exchange Online portion of Office 365
Now that you’ve gotten PowerShell ready to make the connection by running as an administrator, setting the execution policy, and enabling remoting, we next create the session information that we’ll use to connect. If you cut and paste this function into a PS1 file or directly into your PowerShell session you will be ready to connect. Add this function to your PowerShell profile and it will be very easy to connect every time you use PowerShell.
Function Connect-ExchangeOnline {
$office365Credential = Get-Credential
$global:office365= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $office365Credential -Authentication Basic –AllowRedirection
Import-PSSession $office365
}
Once you’ve added this function to your system by either adding it to your profile and restarting PowerShell, adding the function to your current session, or running the PS1 file it was saved into, you can easily connect by typing Connect-ExchangeOnline at the PowerShell prompt. When connecting, you will be prompted by a credential dialog box. Sign in with your Office 365 username and password at that prompt.
The connection is made, and the commands available for administering Exchange Online from the remote session have been imported into your PowerShell session. It’s time to get our list of Exchange Online users. To get the list of users’ mailboxes, we’ll use the command:
Get-Mailbox
The output of which is a list of mailboxes, including the name, alias, server name, and at what size the mailbox will no longer allow email to be sent from the account. Not only are these properties returned, but 179 properties in total, including some really useful properties like “AuditEnabled”, “RetentionPolicy”, “ModerationEnabled”, and “ModerationBy”.
But from the output of the Get-Mailbox command, the one property that seems to be missing is the actual size of the mailbox itself. Instead of using Get-Mailbox to get that information, we can find properties such as the total number of items in a mailbox, and the total size of items in a mailbox, with the Get-MailboxStatistics command.
However, the Get-MailboxStatistics command requires an identity to find. One option for providing the identity is to use this:
Get-MailboxStatistics –identity “Michael”
Or, you can pipe the mailbox identities into the Get-MailboxStatistics command:
Get-Mailbox | Get-MailboxStatistics
We’re almost there. We just need to change which properties are outputted by the Get-MailboxStatistics command.
We’re now able to connect to an Exchange Online session with PowerShell. We’ve used Get-Mailbox to list the users that have mail accounts, and Get-MailboxStatistics to output the default properties
The properties that are displayed by the Get-MailboxStatistics command are DisplayName, ItemCount, StorageLimitStatus, and LastLogonTime. To find which properties may hold the information I want, I pipe the results into Get-Member, which lists all properties and methods for an object.
In particular, there are three properties which I want to have displayed for a usage report: IsArchiveMailbox, ItemCount, and TotalItemSize.
Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName, IsArchiveMailbox, ItemCount, TotalItemSize | Format-Table –autosize
Another option would be to output the usage statistics to a CSV, XML, or to an HTML file. This example creates a CSV file with the usage statistics:
Get-Mailbox | Get-MailboxStatistics | Select-Object DisplayName, IsArchiveMailbox, ItemCount, TotalItemSize | Export-CSV –Path “C:LogsExchangeOnlineUsage.csv”
When you’ve finished administering your Office 365 Exchange mailboxes, you should remove the session. If you close the PowerShell session without closing the remote session to Office 365/Exchange Online, then the remote session will have to wait to time out. An Office 365 account can only have 3 sessions active on it, and it takes 15 minutes for an abandoned session to timeout on it’s own.
While you’re piloting the Office 365 trial, or if you’re an administrator for Office 365, you may find it useful to add this simple function to your profile, and run it when you’re finished with your administrative tasks.
Function Disconnect-ExchangeOnline {
Get-PSSession | Where-Object {$_.ConfigurationName -eq “Microsoft.Exchange”} | Remove-PSSession
}
With this function added to your PowerShell session, you can just enter “Disconnect-ExchangeOnline” to remove all active Exchange Online remote PowerShell sessions.
For performing advanced administrative tasks on Exchange Online/Office 365, you should take advantage of the PowerShell commands that are available on the remote Exchange Online PowerShell servers. To use it, you’ll need to have PowerShell version 2.0 or greater installed, and you’ll need to perform some simple configuration steps before you can connect.
Once connected, it’s very easy to create a simple usage report which tells how large users’ mailboxes are.
After completing your administration of Exchange Online and Office 365 via PowerShell, it’s best to disconnect your session.