Combining PowerShell Cmdlet Results
In my last post I used used New-Object to create an desirable output when the “Get-Mailbox” cmdlet didn’t meet my needs. If your eyes glazed over trying to read the script, let me make it a bit simpler by focusing on a straight forward example.
Say you need to create a list of user’s mailbox size with their email address. This sounds like a simple request, but what you’d soon find is that mailbox sizes are returned with the Get-MailboxStatistics cmdlet and the email address is not. For that, you need to use another cmdlet, such as Get-Mailbox.
With the New-Object cmdlet, we are able to make a custom output that contains data from essentially wherever we want.
See this example:
$MyObject = New-Object PSObject -Property @{
EmailAddress = $null
MailboxSize = $null
}
In this example, I have created a new object with 2 fields, and saved it as the $MyObject variable.
For now, we’ve set the data to null, as shown below:
The next step is to populate each of those fields. We can write to them one at a time with lines like this:
$MyObject.EmailAddress = (Get-Mailbox mcrowley).PrimarySmtpAddress $MyObject.MailboxSize = (Get-MailboxStatistics mcrowley).TotalItemSize
Note: The variable we want to populate is on the left, with what we want to put in it on the right.
To confirm our results, we can simply type the variable name at the prompt:
Pretty cool, huh?
Ok, so now about that list. My example only shows the data for mcrowley, and you probably need more than just 1 item in your report, right?
For this, you need to use the foreach loop. You can read more about foreach here, but the actual code for our list is as follows:
(I am actually going to skip the $null attribute step here)
$UserList = Get-mailbox -Resultsize unlimited
$MasterList = @()
foreach ($User in $UserList) {
$MyObject = New-Object PSObject -Property @{
EmailAddress = (Get-Mailbox $User).PrimarySmtpAddress
MailboxSize = (Get-MailboxStatistics $User).TotalItemSize
}
$MasterList += $MyObject
}
$MasterList
Finally, if you wanted to make this run faster, we really don’t need to run “get-mailbox” twice. For better results, replace the line:
EmailAddress = (Get-Mailbox $User).PrimarySmtpAddress
With this one:
EmailAddress = $User.PrimarySmtpAddress

Hi Mike,
If I wanted to return additional data such as DisplayName, Database, ServerName, itemcount, lastlogontime and {$_.TotalItemSize.Value.ToMB()} how would I add this? also how can you export this to a .csv?
thanks
You need to first get that data from somewhere. Once you have worked out the command do to that, you can add fields to the new-psobject block, and then add lines below the $masterobject.whatever section.
Here is how it would look: https://skydrive.live.com/redir.aspx?cid=0c23cb95e1200929&resid=C23CB95E1200929!1258&parid=C23CB95E1200929!475
Thanks Mike this worked great.
I wrote similar script to help plan for mailbox moves that combines results from Get-Mailbox, Get-User, and Get-MailboxStatistics and save the results into a single CSV file.
See my blog post here for another way to do this:
http://blog.jasonsherry.net/2012/04/13/get-mailboxinfo/
For anyone still reading, here is another approach to this report: http://get-exchange.blogspot.com/2009/03/did-you-ever-needed-to-supply-list-of.html
I think i like my mailbox script that I made up myself. Lemmie know what you think ….
Get-Mailbox | Get-MailboxStatistics | FT @{label=”DisplayName”;expression={$_.DisplayName}}, @{label=”Total Items Count”;
Expression={$_.ItemCount}}, @{label=”Storage Limit”;expression={$_.StorageLimitStatus}}, @{label=”TotalStorageSize”;
Expression={$_.TotalItemSize}}, LastLogonTime -autosize
—————————————
Hello,
Thanks for this script, i’m trying to do the same thing with multi organizations environment.
I wrote that :
$MasterList = @()
foreach($User in Get-Organization) {
$MyObject = New-Object PSObject -Property @{
Name = (Get-Mailbox -Organization $Org).displayname
}
$MasterList += $MyObject
}
$MasterList | Export-CSV -Path c:\test.csv -Delimiter “;” -Notype
But my CSV file looks like (no value) :
“Name”
Can you help me ?
Is this a continuation from http://powershellcommunity.org/Forums/tabid/54/aft/8700/Default.aspx ?
Unfortunately I don’t have a multi-tenant org to test this with, so I’m not sure what the output of get-organization is.
At a glance, I wonder where $org is defined. Should that be $user?
One-liner, can be customized to add anything you want:
Get-Mailbox -Identity | Select-Object -Property PrimarySmtpAddress,@{Name=”MailboxSize”;Expression={ $_ | Get-MailboxStatistics | Select-Object -ExpandProperty TotalItemSize }}
the Identity parameter was wiped out form my previous post, but you get the idea