Powershell Script not returing all collumns

I'm trying to run a powershell script for Exchange 2007 on a Windows 2008 R2 server to report on all users. It runs but I don't get any output for the Inbox and Sent box.

I have one problem. One user has two accounts with different email addresses but the same display name. I need the script to handle that situation also.

Here is the script I'm running.
Get-User -ResultSize Unlimited -Filter 'RecipientType -eq "UserMailbox"' | Select DisplayName | foreach {
$MailboxInfo = Get-Mailbox -identity $_.displayName
$MailboxStat = Get-MailboxStatistics -identity $_.displayName
Add-Member -InputObject $_ noteProperty UseDefault $MailboxInfo.UseDatabaseQuotaDefaults
Add-Member -InputObject $_ noteProperty WarningQuota $MailboxInfo.IssueWarningQuota
Add-Member -InputObject $_ noteProperty SendQuota $MailboxInfo.ProhibitSendQuota
Add-Member -InputObject $_ noteProperty SendRecieveQutoa $MailboxInfo.ProhibitSendReceiveQuota

Add-Member -InputObject $_ noteProperty QuotaStatus $MailboxStat.StorageLimitStatus
Add-Member -InputObject $_ noteProperty TotalItems $MailboxStat.ItemCount
Add-Member -InputObject $_ noteProperty TotalSizeMB $MailboxStat.TotalItemSize.Value.ToMB()
Add-Member -InputObject $_ noteProperty Inbox $MailboxStat.InboxItemCount
Add-Member -InputObject $_ noteProperty SentItems $MailboxStat.SentItemCount
Add-Member -InputObject $_ noteProperty DeletedItems $MailboxStat.DeletedItemCount
Add-Member -InputObject $_ noteProperty DeletedSizeMB $MailboxStat.TotalDeletedItemSize.Value.ToMB() -PassThru
} | Export-Csv -Path C:\PS_Rpts\MailboxSize-6-26-2015-Test9.csv -NoTypeInformation
LVL 25
Tony GiangrecoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Murali ReddyExchange ExpertCommented:
Instead of display name choose a unique attribute some thing like SamAccountName or primarysmtpaddress.
Get-MailboxStatistics wont give you mailbox folder items count, you need to use Get-MailboxFolderStatistics

$Mbxs = Get-Mailbox -ResultSize Unlimited
$(Foreach ($MB in $Mbxs){
 $MailboxStat = $MB | Get-MailboxStatistics
 New-Object PSObject -Property @{
 DisplayName = $MB.DisplayName
 UseDefault = $MB.UseDatabaseQuotaDefaults
 WarningQuota = $MB.IssueWarningQuota
 SendQuota = $MB.ProhibitSendQuota
 SendRecieveQutoa = $MB.ProhibitSendReceiveQuota
 QuotaStatus = $MailboxStat.StorageLimitStatus 
 TotalItems = $MailboxStat.ItemCount
 TotalSizeMB = $MailboxStat.TotalItemSize.Value.ToMB()
 Inbox = ($MB | Get-MailboxFolderStatistics -FolderScope Inbox).ItemsInFolder
 SentItems = ($MB | Get-MailboxFolderStatistics -FolderScope SentItems).ItemsInFolder
 DeletedItems = ($MB | Get-MailboxFolderStatistics -FolderScope DeletedItems).ItemsInFolder
 DeletedSizeMB = $MailboxStat.TotalDeletedItemSize.Value.ToMB()
 }) | Export-Csv -Path C:\PS_Rpts\MailboxSize-6-26-2015-Test9.csv -NoTypeInformation

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tony GiangrecoAuthor Commented:
Thanks for the help. That works better.

I tried making modifications but keep getting errors.

Is it possible to just have the following columns and have the time stamp dd/mm/yy: time on the first line and in the file name?

Employee Name, Inbox items, inbox Size, Deleted Items, Deleted Item Size, Sent Items, Sent Item Size, Total Mailbox size

Thanks in advance for the help!
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

You can use Select-Object command to get required properties in report.. for example. To select DisplayName,TotalItems and Inbox in report and add the current date in report file..  change last line of code to..
 }) | Select-Object DisplayName,TotalItems,Inbox | Export-Csv -Path "C:\PS_Rpts\MailboxSize-$(Get-Date -f dd-MM-yyy)-Test9.csv" -NoTypeInformation

Open in new window

Tony GiangrecoAuthor Commented:
I have about ten employees that I don't want listed in this report. Can anyone tell me how to exclude them in the powershell script?
Enter the samaccountname of users which you want to exclude in C:\exclude.txt and replace the first line of code with following..

$exclude = GC C:\exclude.txt
$Mbxs = Get-Mailbox -ResultSize Unlimited | ?{$exclude -notcontains $_.samaccountname}

Open in new window

Tony GiangrecoAuthor Commented:
Hi Subsun,

Thanks for the help. I hadn't received a reply for a few days so i began working on a new script. this is what I have, but I need to add a column for the Deleted and Sent boxes showing items and size for each along with excluding the ten employees mentioned above.

I assume the exclude.txt file would list list the employee names like this

Can you assist with this script?

$mailboxes = @(Get-Mailbox -ResultSize Unlimited)
$report = @()

foreach ($mailbox in $mailboxes)
    $inboxstats = Get-MailboxFolderStatistics $mailbox -FolderScope Inbox | Where {$_.FolderPath -eq "/Inbox"}

    $mbObj = New-Object PSObject
    $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
    $mbObj | Add-Member -MemberType NoteProperty -Name "Inbox Size (Mb)" -Value $inboxstats.FolderandSubFolderSize.ToMB()
    $mbObj | Add-Member -MemberType NoteProperty -Name "Inbox Items" -Value $inboxstats.ItemsinFolderandSubfolders
    $report += $mbObj

C:\exclude.txt should contain the user name (samaccountname) of user..


Open in new window

Code will again same.. replace the fist line with following..
$exclude = GC C:\exclude.txt
$mailboxes  = Get-Mailbox -ResultSize Unlimited | ?{$exclude -notcontains $_.samaccountname}

Open in new window

And if you want to use the email address of user to exclude.. then C:\exclude.txt should contain the email address of user..


Open in new window

and replace the fist line with following..
$exclude = GC C:\exclude.txt
$mailboxes  = Get-Mailbox -ResultSize Unlimited | ?{$exclude -notcontains $_.Primarysmtpaddress}

Open in new window

Tony GiangrecoAuthor Commented:
Hi Subsun,

Thanks for your suggestions.  I've experimented with different scripts and have this one working except it does not filter the users out of the report. I've tried by entering one email address per line and also tied one AD username per line. They still appear on the report.

Here is what I've tried
$exclude = GC C:\PS-Scripts\exclude2.txt
Get-Mailbox -ResultSize Unlimited |"

Here is the script without the exclude option.
Get-Mailbox -ResultSize Unlimited |
ForEach-Object {
$mbx = $_ | Select DisplayName
$Inboxstats = Get-MailboxFolderStatistics $_ -FolderScope Inbox | Where {$_.foldertype -eq "Inbox"} | Select ItemsInFolder, FolderSize
$SentItemsstats = Get-MailboxFolderStatistics $_ -FolderScope SentItems | Where {$_.foldertype -eq "SentItems"} | Select ItemsInFolder, FolderSize
$DeletedItemsstats = Get-MailboxFolderStatistics $_ -FolderScope DeletedItems | Where {$_.foldertype -eq "DeletedItems"} | Select ItemsInFolder, FolderSize

Get-MailboxStatistics $_ | ForEach {
$mbx | Add-Member -MemberType NoteProperty -Name "Mailbox Total Size (MB)" -Value $_.TotalItemSize.Value.ToMB()
$mbx | Add-Member -MemberType NoteProperty -Name "Mailbox Total Item Count" -Value $_.ItemCount
$mbx | Add-Member -MemberType NoteProperty -Name "Inbox Total Size (MB)" -Value $InboxStats.FolderSize.ToMB()
$mbx | Add-Member -MemberType NoteProperty -Name "Inbox Total Item Count" -Value $Inboxstats.ItemsInFolder
$mbx | Add-Member -MemberType NoteProperty -Name "SentItems Total Size (MB)" -value $SentItemsstats.FolderSize.ToMB()
$mbx | Add-Member -MemberType NoteProperty -Name "SentItems Total Item Count" -value $SentItemsstats.ItemsInFolder
$mbx | Add-Member -MemberType NoteProperty -Name "DeletedItems Total Size (MB)" -value $DeletedItemsstats.FolderSize.ToMB()
$mbx | Add-Member -MemberType NoteProperty -Name "DeletedItems Total Item Count" -value $DeletedItemsstats.ItemsInFolder
} | export-csv -notypeinformation C:\PS_Rpts\Good-Horizontal-7515-v2.txt
This is a different script, not the method you posted earlier.. if you could post the script which you want to use then i can tell you on what to modify..
Tony GiangrecoAuthor Commented:
This script creates the type of report I need. Is there a way to use the exclude file with it?

$exclude = GC C:\exclude.txt
Get-Mailbox -ResultSize Unlimited | ?{$exclude -notcontains $_.Primarysmtpaddress} |
Foreach-Object....(Continue as per your code)
Tony GiangrecoAuthor Commented:
I ran into another problem with this script. I've been testing it and discovered the counts include all email in sub-folders. How do I exclude sub-folders with this script?
When you run Get-MailboxFolderStatistics command, ItemsInFolder property will give only items in the folder. ItemsInFolderAndSubfolders property will give the total items including subfolders..
Tony GiangrecoAuthor Commented:
Thanks for the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.