Exchange Get-MailboxStatistics with user Alias

Exchange has a nice PowerShell cmdlet called Get-MailboxStatistics, which by default, will output DisplayName, ItemCount, StorageLimitStatus, and LastLogonTime — like so:

1
2
3
4
5
[PS] C:\temp>Get-Mailbox -Identity user@domain.org | Get-MailboxStatistics | Format-Table -AutoSize
 
DisplayName   ItemCount StorageLimitStatus       LastLogonTime
-----------   --------- ------------------       -------------
Last, First 29665             NoChecking 2/4/2016 9:57:33 PM

Well, if you want to easily compare results to Users in Active Directory, having the SamAccountName or Alias would be nice. But the Get-MailboxStatistics cmdlet only outputs the DisplayName, not the SamAccountName or Alias.

Thanks to guidance from Robbie Roberts and Gavin Woodall, I came up with the following PowerShell script that will output the user’s Alias/SamAccountName, DisplayName, ItemCount (in MB), and LastLogonTime in a pipe-delimited format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Get a list of all Exchange users
$Users = Get-Mailbox -ResultSize unlimited
 
# Do the following for each user
foreach ($User in $Users)
{
 
	# Get the user's alias and display name
	$UserAlias = $User.Alias
	$UserDisplayName = $User.DisplayName
 
	# Get the user's mailbox statistics on ItemCount
	$UserItemCount = Get-MailboxStatistics $UserDisplayName | Select-Object ItemCount
	$UserItemCount = $UserItemCount.ItemCount
 
	# Get the user's mailbox statistics on TotalItemSize, format to just MB
	$UserTotalItemSize = Get-MailboxStatistics $UserDisplayName | Select-Object TotalItemSize
	$UserTotalItemSize = $UserTotalItemSize.TotalItemSize.Value.toMB()
 
	# Get the user's mailbox statistics on LastLogonTime
	$UserLastLogonTime = Get-MailboxStatistics $UserDisplayName | Select-Object LastLogonTime
	$UserLastLogonTime = $UserLastLogonTime.LastLogonTime
 
	# Combine results into a pipe-delimited string
	$OutputString = $UserAlias + "|" + $UserDisplayName + "|" + $UserItemCount + "|" + $UserTotalItemSize + "|" + $UserLastLogonTime
 
	# Display the results on the screen to show progress
	Write-Host $OutputString
 
	# Write the results to a .csv file
	$OutputString | Out-File C:\temp\LastLogonWithAlias.csv -Append
 
}

The result looks something like this:

adamsa|Adams, Aaron|675|87|02/04/2016 15:05:50
bealb|Beal, Britt|440|56|02/02/2016 11:53:21
chandlerc|Chandler, Chris|1867|128|08/19/2014 17:17:02
duked|Duke, David|518|40|01/22/2016 22:22:41

I can now use Microsoft Excel to Covert Text to Columns using the pipe as a delimiter.