We help IT Professionals succeed at work.

Piping multiple cmdlets into a single CSV file?

RoboMunch asked
I need to get some information from three different cmdlets and I'm having some trouble. I'm trying to pull it from these:

Get-User (FirstName & LastName)
Get-Mailbox (SamAccountName)
Get-MailboxStatistics (ItemCount & TotalItemSize)

Is there a way to get all of thes into a single .csv file?

Thanks in advance for the help!
Watch Question

IT Security Architect
$Users = Get-User -ResultSize unlimited
$Output = @()
foreach ($User in $Users){
   $Mailbox = Get-Mailbox $User.DistinguishedName
   $MailboxStatistics = Get-MailboxStatistics $User.DistinguishedName
   $objUser = New-Object System.Object
   $objUser | Add-Member -MemberType NoteProperty -Name "FirstName" -Value $User.FirstName
   $objUser | Add-Member -MemberType NoteProperty -Name "LastName" -Value $User.Lastname
   $objUser | Add-Member -MemberType NoteProperty -Name "SamAccountName" -Value $Mailbox.SamAccountName
   $objUser | Add-Member -MemberType NoteProperty -Name "ItemCount" -Value $MailboxStatistics.ItemCount
   $objUser | Add-Member -MemberType NoteProperty -Name "TotalItemSize" -Value $MailboxStatistics.TotalItemSize.ToString()
   $Output += $objUser

$Output | Select-Object FirstName,LastName,SamAccountName,ItemCount,TotalItemSize | Export-CSV "Output.csv"

Open in new window

This will take a while to process if you have alot of users.
Top Expert 2011


The following will return the details only for users with mailbox.
"SamAccountName,FirstName,LastName,ItemCount,Size(MB)" | Out-File c:\output.csv

$mbxs=get-mailbox -resultsize unlimited | select identity,SamAccountName

foreach ($mbx in $mbxs){
	$user=get-user -identity $mbx.identity |
	select FirstName,LastName
	$mbxst=get-mailboxstatistics -identity $mbx.identity |
	select ItemCount,TotalItemSize
	Write-Host "$aname,$fname,$lname,$icount,$size"
	"$aname,$fname,$lname,$icount,$size" | Out-File c:\output.csv -Append	


Open in new window

steforIT Security Architect

Or you could just switch the first row in the first response to whatever you like.
$Users = Get-Mailbox -ResultSize unlimited | where {$_.RecipientTypeDetails -eq "UserMailbox"} | Get-User

Open in new window

Will return only users with user mailboxes for instance.


Thanks for the replies!

There are about 15,000 mailboxes in this environment, so I was hoping to break it down by server (there are 4) or even database. I'm assuming this would work, correct?

$Users = Get-Mailbox -Server XXXXXXX...

Open in new window

$Users = Get-Mailbox -Database XXXXXXX...

Open in new window

Yep. Alternatively when getting all mailboxes the Get-Mailbox command should contain the DB's/Server names so you can just include those fields in your report.

I think stefor's solution is better as it uses the arrays properly as opposed to the other one you were given.

Though I don't know why he bothered to filter the $output results with the Select statement, just put whatever you want into the array and then just pipe that to Export-Csv.


Awesome script, worked perfectly...thanks!