Skip to content

Instantly share code, notes, and snippets.

@junecastillote
Last active March 18, 2019 14:08
Show Gist options
  • Save junecastillote/3d43a1017fb3cab4a463c2e0e964d564 to your computer and use it in GitHub Desktop.
Save junecastillote/3d43a1017fb3cab4a463c2e0e964d564 to your computer and use it in GitHub Desktop.
Combining Selected Properties of Two Objects into a New Array using PowerShell
#create an empty array for the final result
$finalResult = @()
#get mailboxes as per your requirement
$mailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited | Sort-Object UserPrincipalName
#loop through each object
foreach ($mailbox in $mailboxes) {
Write-Host "Reading:" $mailbox.UserPrincipalName
#define your custom object the quick and dirty way, and select whichever properties to include
$temp = "" | Select DisplayName,PrimarySmtpAddress, ItemCount,TotalItemSizeInBytes,ArchiveStatus,LitigationHoldEnabled
#hold the MailboxStatistics for each mailbox in this variable
$stats = Get-MailboxStatistics -Identity $mailbox.UserPrincipalName
#assemble your object
$temp.DisplayName = $mailbox.DisplayName
$temp.PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
$temp.itemCount = $stats.ItemCount
$temp.TotalItemSizeInBytes = [math]::Round(($stats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")),2)
$temp.ArchiveStatus = $mailbox.ArchiveStatus
$temp.LitigationHoldEnabled = $mailbox.LitigationHoldEnabled
#add object to array
$finalResult += $temp
}
#Display result
$finalResult | Format-Table -autosize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment