Link to home
Start Free TrialLog in
Avatar of RoboMunch
RoboMunch

asked on

Piping multiple cmdlets into a single CSV file?

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!
ASKER CERTIFIED SOLUTION
Avatar of stefor
stefor
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
	
	$aname=$mbx.Samaccountname
	$fname=$user.FirstName
	$lname=$user.LastName
	$icount=$mbxst.ItemCount
	$size=$mbxst.TotalItemSize.value.ToMB()
	
	Write-Host "$aname,$fname,$lname,$icount,$size"
	"$aname,$fname,$lname,$icount,$size" | Out-File c:\output.csv -Append	

}

Open in new window

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.
Avatar of RoboMunch
RoboMunch

ASKER

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

or
$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!