Skip to content

Instantly share code, notes, and snippets.

@Trimad
Last active November 17, 2021 20:29
Show Gist options
  • Save Trimad/23d1f60c518fda9b4265edfe0456d8d9 to your computer and use it in GitHub Desktop.
Save Trimad/23d1f60c518fda9b4265edfe0456d8d9 to your computer and use it in GitHub Desktop.
Combining Cmdlets
# Connect to Exchage Online Management
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
#Connect to Azure Active Directory
Import-Module AzureAD
Connect-AzureAD
$ExchangeOnlineManagementUsers = Get-Mailbox
#Run this to test that $ExchangeOnlineManagementUsers actually contains data:
#Foreach ($user in $ExchangeOnlineManagementUsers) {Write-Output $user | select *}
$AzureUsers = Get-AzureADUser
#Run this to test that $AzureUsers actually contains data:
#Foreach ($user in $AzureUsers) {Write-Output $user}
$Result=@()
Foreach ($a in $ExchangeOnlineManagementUsers) { #For each user in Exchange Online Management,
Foreach ($b in $AzureUsers) { #for each user in Microsoft Online,
if([string]$a.UserPrincipalName -eq [string]$b.UserPrincipalName){ #if their user principal names match,
$Result += $obj = [PSCustomObject]@{ #create a custom PowerShell Object with properties from both EOM and MSO,
#Alias = $a.Alias
DisplayName = $a.DisplayName
WhenMailboxCreated = $a.WhenMailboxCreated
PrimarySmtpAddress = $a.PrimarySmtpAddress
MailboxPlan = $a.MailboxPlan
DirSyncEnabled = $b.DirSyncEnabled
LastDirSyncTime = $b.LastDirSyncTime
}
#Write-Output $obj #then write that object to the console.
}
}
}
$Result | Export-CSV "output.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment