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!
LVL 1
RoboMunchAsked:
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.

steforCommented:
$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.
0

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
prashanthdCommented:

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

0
steforCommented:
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.
0
MSSPs - Are you paying too much?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

RoboMunchAuthor Commented:
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

0
chrismerrittCommented:
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.
0
RoboMunchAuthor Commented:
Awesome script, worked perfectly...thanks!
0
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
Powershell

From novice to tech pro — start learning today.