Exchange Script to combine output from multiple commands into a single CSV file.
Scenario: You want to combine information from the output of different Exchange Shell commands into a single CSV file. The three commands I am going to combine are below.
get-user (This holds user specific info)
get-mailbox (This holds mailbox specific info)
get-mailboxstatistics (this holds mailbox statistics specific info)
Script:
$mailboxes = get-mailbox -resultsize unlimited | Where Database -like "EXCHDB*"
$mailboxes = $mailboxes | Sort alias
$mailboxes | Foreach-Object{
$user = Get-User $_.name
$mbx = Get-Mailbox $_.name
$mbxstat = Get-MailboxStatistics $_.name
Write-Host $user
New-Object -TypeName PSObject -Property @{
FirstName = $user.FirstName
LastName = $user.LastName
DisplayName = $user.DisplayName
Title = $user.title
Department = $user.Department
Office = $user.Office
Manager = $user.manager
Alias = $mbx.Alias
Database = $mbx.database
Servername = $mbx.servername
OrganizationalUnit = $mbx.organizationalunit
TotalItemSize = $mbxstat.totalitemsize
TotalItemSizeInMB = $mbxstat | Select {$_.TotalItemSize.Value.ToMB()}
PrimarySMTPAddress = $mbx.primarysmtpaddress
}
} | Export-csv C:\output\MailboxAndUserInfo.csv
get-user (This holds user specific info)
get-mailbox (This holds mailbox specific info)
get-mailboxstatistics (this holds mailbox statistics specific info)
Script:
$mailboxes = get-mailbox -resultsize unlimited | Where Database -like "EXCHDB*"
$mailboxes = $mailboxes | Sort alias
$mailboxes | Foreach-Object{
$user = Get-User $_.name
$mbx = Get-Mailbox $_.name
$mbxstat = Get-MailboxStatistics $_.name
Write-Host $user
New-Object -TypeName PSObject -Property @{
FirstName = $user.FirstName
LastName = $user.LastName
DisplayName = $user.DisplayName
Title = $user.title
Department = $user.Department
Office = $user.Office
Manager = $user.manager
Alias = $mbx.Alias
Database = $mbx.database
Servername = $mbx.servername
OrganizationalUnit = $mbx.organizationalunit
TotalItemSize = $mbxstat.totalitemsize
TotalItemSizeInMB = $mbxstat | Select {$_.TotalItemSize.Value.ToMB()}
PrimarySMTPAddress = $mbx.primarysmtpaddress
}
} | Export-csv C:\output\MailboxAndUserInfo.csv