Skip to content

Instantly share code, notes, and snippets.

@hansgafriedzal
Last active September 14, 2022 16:26
Show Gist options
  • Save hansgafriedzal/1161d77353c2b53b7b99bbc60aee8247 to your computer and use it in GitHub Desktop.
Save hansgafriedzal/1161d77353c2b53b7b99bbc60aee8247 to your computer and use it in GitHub Desktop.
$timestamp = Get-Date -format "yyyyMMddHHmmss"
Get-MgUser -All `
-Filter 'accountEnabled eq true' `
-Property AccountEnabled, OnPremisesUserPrincipalName, UserPrincipalName, DisplayName, CompanyName, UserId `
-ExpandProperty Manager | foreach{
[pscustomobject] @{
Id = $_.Id
UserPrincipalName = $_.UserPrincipalName
DisplayName = $_.DisplayName
CompanyName = $_.CompanyName
AccountEnabled = $_.AccountEnabled
ManagerId = $_.Manager.Id
ManagerUserPrincipalName = $_.Manager.AdditionalProperties.userPrincipalName
ManagerDisplayName = $_.Manager.AdditionalProperties.displayName
ManagerAccountEnabled = $_.Manager.AdditionalProperties.accountEnabled
ManagerCompanyName = $_.Manager.AdditionalProperties.companyName
} | where ManagerUserPrincipalName -NE $null
} | Export-Csv "Get-MgUser_$timestamp.csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment