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:outputMailboxAndUserInfo.csv

Advertisement

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

%d bloggers like this: