Get-ADUser -Filter

Glenn Maxwell 12,766 Reputation points
2025-05-07T19:58:03.7933333+00:00

Hi all, when I use the syntax below, I get the output for the user's manager. However, I also want to include the manager's email in the output. Please guide me on how to retrieve this information

$query ="(userAccountControl -like 512)" 
Get-ADUser -Filter $query -Properties DisplayName,EmailAddress,Userprincipalname,employeeNumber,employeeType,manager | Select DisplayName,EmailAddress,Userprincipalname,employeeNumber,employeeType,@{name='Manager';expression={$_.manager -match '(?<=CN=).+?(?=,)'|Out-Null;$Matches.Values}}  | Export-csv C:\temp\output.csv -Notypeinformation

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,959 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hien-L 4,180 Reputation points Microsoft External Staff Moderator
    2025-05-08T03:24:03.6933333+00:00

    Hi Glenn Maxwell,

    Here is a post with the similar requirement, that need to get manager’s email for AD users. Please check if those provided scripts could be helpful:

    Help with a script that pulls list of AD users and returns manager info - Programming & Development - Spiceworks Community

     Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Glenn Maxwell 12,766 Reputation points
    2025-05-08T05:06:34.2533333+00:00

    i tried below but i did not get the output.

    i have csv file in below format.
    sam
    user1
    user2
    
    $requestedUsers = Import-Csv "C:\temp\input.csv"
    
    $allUsers = Get-ADUser -filter 'Enabled -eq $true' -Properties Displayname, EmailAddress, Manager
    
    $filterdUsers = $allUsers |
    Where-Object { $_.SamAccountName -in $requestedUsers.sam }  
    $report = foreach ($user in $filterdUsers) {
        $managerEmail = $allUsers |
        Where-Object DistinguishedName -eq $user.Manager |
        Select-Object -ExpandProperty EmailAddress
    
        [PSCustomObject][ordered]@{
            DisplayName  = $user.DisplayName
            EmailAddress = $user.EmailAddress
            ManagerEmail = $managerEmail
        }
    }
    
    $report | export-csv c:\temp\output.csv -Notypeinformation
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.