Export User and User’s manager info to a CSV file


I had to export members of a group, and each member’s manager into a CSV file to generate a specific report. I wrote a quick PowerShell command for On-Premises Active Directory and for Azure AD.

Here is the PowerShell command for On-Premises Active Directory. Replace GroupName with your own security group name in the beginning of the command and CSV file name in the end. This command will create a CSV file with “Username, Email, ManagerUsername, ManagerEmail”.

Import-Module ActiveDirectory

Get-ADGroupMember “GroupName” | % { Get-ADUser -Identity $_.SamAccountName -Properties Mail,manager | Select @{N=”Username”;E={$_.SamAccountName}},@{N=”Email”;E={$_.mail}},@{N=”ManagerUsername”;E={(Get-ADUser $_.Manager).SamAccountName}},@{N=”ManagerEmail”;E={(Get-ADUser $_.Manager -Properties mail).mail}} } | Export-Csv -Path .\GroupName.csv -NoTypeInformation

Here is the Microsoft Graph command to get the same result as above from Azure AD aka Entra.

Connect-MgGraph -Scopes Directory.AccessAsUser.All, Directory.Read.All,Group.Read.All

Get-MgGroupMember -GroupId (Get-MgGroup -Filter “DisplayName eq ‘GroupName'”).Id | select @{N=”DisplayName”;E={(Get-MgUser -UserId $_.Id).DisplayName}},@{n=”Email”;E={(Get-MgUser -UserId $_.id).mail}},@{N=”Manager Name”;E={ (Get-MgUser -UserId (Get-MgUserManager -UserId $_.Id).Id).DisplayName}},@{N=”Manager Email”;E={ (Get-MgUser -UserId (Get-MgUserManager -UserId $_.Id).Id).mail}} | Export-Csv -Path .\GroupName.csv -NoTypeInformation

I do hope it helps you.

Leave a comment