Home > Active Directory, Exchange Server, Office365 > Combining PowerShell Cmdlet Results

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:

$MyObject

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:

$MyObject with data

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

$MasterList with data

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
  1. Joe
    April 26, 2012 at 11:43 am | #1

    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

  2. May 1, 2012 at 6:56 pm | #4

    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/

  3. May 14, 2012 at 10:36 pm | #5

    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

    • September 1, 2012 at 9:30 pm | #6

      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

      —————————————

  4. Jo
    July 14, 2012 at 10:29 am | #7

    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 ?

  5. April 22, 2013 at 7:34 am | #9

    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 }}

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 37 other followers

%d bloggers like this: