# Exchange 2003 item counts with Powershell

### Question

• I've got a script that'll take some Exchange 2003 server names and give me back some info about the mailboxes on those servers (code below). I've added "ItemCount," but that value is coming back blank. Anyone have an idea what I'm doing wrong and how I can fix this? Thanks.

$CSVFilePath = 'c:\it\scripts\mailboxReport.csv'$strDate = get-date -uformat "%d %b %Y"
"Report run: " + $strDate | out-file -filepath$CSVFilePath -encoding ascii
$strOutputString = "Display Name,Mailbox Server,Storage Group,Database,Size (KB),Item Count,Last Logon Time"$strOutputString | out-file -filepath $CSVFilePath -encoding ascii -append #Exchange 2003 servers$computers = "amusdhcaimex01","amusdhcaimex02","amusdhcaimex03","grvs153","grvs154","grvs155","grvs156","grvs159","grvs190","grvs191","grvs192","grvs194"
foreach ($computer in$computers) {
$users = Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer$computer | Select-Object MailBoxDisplayName,StorageGroupName,StoreName,Size,ItemCount,LastLogonTime
foreach ($user in$users) {
$date= [string]$user.LastLogonTime
if ($date.length -eq 0) {$strOutputString = """"+ $user.MailBoxDisplayName + """, " +$computer + ", " + $user.StorageGroupName + ", " +$user.StoreName + ", " + $user.Size + ", " +$user.ItemCount + ", N/A"
}
else {
$strOutputString = """"+$user.MailBoxDisplayName + """, " + $computer + ", " +$user.StorageGroupName + ", " + $user.StoreName + ", " +$user.Size + ", " + $user.ItemCount +"dur"+ ", " +$date.substring(4,2)+"/"+$date.substring(6,2) +"/"+$date.substring(0,4)
}
$strOutputString | out-file -filepath$CSVFilePath -encoding ascii -append
}
}

Friday, March 16, 2012 6:48 PM

### All replies

• I should add that I put the "dur" in there for testing, and I don't see it in the CSV.
Friday, March 16, 2012 6:51 PM
• Use PowerShell to generate your CSV file.

$CSVFilePath = 'c:\it\scripts\mailboxReport_YYYYMMDD.csv'$CSVFilePath = $CSVFilePath -replace 'YYYYMMD',(Get-Date -f 'yyyMMdd')$computers = "amusdhcaimex01","amusdhcaimex02","amusdhcaimex03","grvs153","grvs154","grvs155","grvs156","grvs159","grvs190","grvs191","grvs192","grvs194"
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computers | Select-Object MailBoxDisplayName,StorageGroupName,StoreName,Size,ItemCount,LastLogonTime | Export-Csv$CSVFilePath -NoType

If you put the date in the first row the file will not be usable as a CSV file.  Put the date into name.

¯\_(ツ)_/¯

• Edited by Friday, March 16, 2012 7:43 PM
Friday, March 16, 2012 7:35 PM
• If we set up PowerShell correctly we can even do HTML reports like this.

$HTMLFilePath = 'c:\scripts\mailboxReport_YYYYMMDD.htm'$HTMLFilePath = $HTMLFilePath -replace 'YYYYMMD',(Get-Date -f 'yyyMMdd')$computers = "amusdhcaimex01","amusdhcaimex02","amusdhcaimex03","grvs153","grvs154","grvs155","grvs156","grvs159","grvs190","grvs191","grvs192","grvs194"
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computers | Select-Object MailBoxDisplayName,StorageGroupName,StoreName,Size,ItemCount,LastLogonTime | ConvertTo-Html | Out-File$HTMLFilePath
& \$HTMLFilePath

If you run this you will get a nice HTML report in the browser or it can be inserted into a mail message body.  The report can have a style sheet attacted and even be grouped and sorted.

¯\_(ツ)_/¯

Friday, March 16, 2012 8:07 PM
• This link doesn't document an ItemCount property:

http://msdn.microsoft.com/en-us/library/aa143732(v=EXCHG.65).aspx

Do you want the TotalItems property?

Richard Mueller - MVP Directory Services

• Marked as answer by Tuesday, March 20, 2012 6:17 PM
Tuesday, March 20, 2012 4:34 PM
• I thought I had tried TotalItems, but it worked this time so, thanks.

Tuesday, March 20, 2012 6:17 PM