Powershell Task of the Day – Get Mailbox Size For Users in an OU

During an email migration, I wanted to see how many users would be under the new limits that would be imposed on them. In most cases, the restrictions would match their location in Active Directory. So I was able to export the needed details and view them:

# Process email stats for users in the specified OU and save to CSV file

Add-PSSnapin Quest.ActiveRoles.ADManagement
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Support

if (!$args) # Empty arguments
write-host "You must specify correct arguments, specify the OU to check, then the file name. ie. email_stats.ps1 domain.com/OUName/OUName2 oulist.csv"

write-host "Processing based on arguments..."
write-host "OU: " $args[0]
write-host "CSV: " $args[1]

$OUName = $args[0]
$FileName = $args[1]
$strTest = "Name,Alias,ServerName,OU,TotalSize(KB)`n"

$arrMailboxList = get-mailbox -OrganizationalUnit $OUName | select name, alias,servername,organizationalunit
$arrMailBoxListCount = $arrMailboxList.count
foreach ($item in $arrMailboxList)
$i = $i+1
write-progress -id 1 -activity "Getting Mailbox List" -status "Progress:" -percentcomplete ($i/$arrMailboxListCount*100)
$arrMailboxStats = Get-MailboxStatistics -identity $item.alias | select displayname,totalitemsize
foreach ($stat in $arrMailboxStats)
$strTest = $strTest + $item.name + "," + $item.alias + "," + $item.servername + "," + $item.organizationalunit + "," + $stat.totalitemsize.value.toKB() + "`n"

Out-File -filePath $FileName -inputObject $strTest -encoding ASCII

This is my first powershell script using arguments. In this script, I was wanting to submit 2 arguments – the Organisation Unit to check and the CSV file to save the final results to. The basic work flow is to get all the users in the specified OU and then loop through the results, performing a Get-MailboxStatistics cmdlet on each. A CSV-formatted string is stored in a variable and then finally outputted to a CSV file.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.