Hi all
\nI have a list of users in a CSV file, and I want to retrieve their managers’ email addresses. However, when I use the syntax below, I don’t get any output. I have both UPNs and sAMAccountNames, and the CSV file contains the sAMAccountNames.<\/p>\n
i have csv file in below format.\nsam\nuser1\nuser2\n\n$requestedUsers = Import-Csv \"C:\\temp\\input.csv\"\n\n$allUsers = Get-ADUser -filter 'Enabled -eq $true' -Properties Displayname, EmailAddress, Manager\n\n$filterdUsers = $allUsers |\nWhere-Object { $_.SamAccountName -in $requestedUsers.sam } \n$report = foreach ($user in $filterdUsers) {\n $managerEmail = $allUsers |\n Where-Object DistinguishedName -eq $user.Manager |\n Select-Object -ExpandProperty EmailAddress\n\n [PSCustomObject][ordered]@{\n DisplayName = $user.DisplayName\n EmailAddress = $user.EmailAddress\n ManagerEmail = $managerEmail\n }\n}\n\n$report | export-csv c:\\temp\\output.csv -Notypeinformation\n<\/code><\/pre>","upvoteCount":5,"answerCount":2,"datePublished":"2025-05-08T10:07:00.783Z","author":{"@type":"Person","name":"Glenn-Maxwell","url":"https://community.spiceworks.com/u/Glenn-Maxwell"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
Instead of filtering $allUsers<\/code> again, use Get-ADUser<\/code> to directly retrieve the manager’s details using their distinguished name.<\/p>\n
Advertisement
Initialize $report<\/code> as an array before the loop to store the results<\/p>\nSee one I wrote earlier<\/p>\n
# Import the CSV file\n$requestedUsers = Import-Csv \"C:\\temp\\input.csv\"\n\n# Retrieve all enabled users with the necessary properties\n$allUsers = Get-ADUser -filter 'Enabled -eq $true' -Properties DisplayName, EmailAddress, Manager\n\n# Filter users based on sAMAccountName from the CSV file\n$filteredUsers = $allUsers | Where-Object { $_.SamAccountName -in $requestedUsers.sam }\n\n# Initialize an array to store the report\n$report = @()\n\n# Loop through each filtered user to get their manager's email address\nforeach ($user in $filteredUsers) {\n $manager = Get-ADUser -Identity $user.Manager -Properties EmailAddress\n $managerEmail = $manager.EmailAddress\n # Create a custom object for the report\n $report += [PSCustomObject][ordered]@{\n DisplayName = $user.DisplayName\n EmailAddress = $user.EmailAddress\n ManagerEmail = $managerEmail\n }\n}\n\n# Export the report to a CSV file\n$report | Export-Csv -Path \"C:\\temp\\output.csv\" -NoTypeInformation\n\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2025-05-08T10:54:41.893Z","url":"https://community.spiceworks.com/t/fetch-managers-email-address/1203741/2","author":{"@type":"Person","name":"Rod-IT","url":"https://community.spiceworks.com/u/Rod-IT"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi all
\nI have a list of users in a CSV file, and I want to retrieve their managers’ email addresses. However, when I use the syntax below, I don’t get any output. I have both UPNs and sAMAccountNames, and the CSV file contains the sAMAccountNames.<\/p>\n
i have csv file in below format.\nsam\nuser1\nuser2\n\n$requestedUsers = Import-Csv \"C:\\temp\\input.csv\"\n\n$allUsers = Get-ADUser -filter 'Enabled -eq $true' -Properties Displayname, EmailAddress, Manager\n\n$filterdUsers = $allUsers |\nWhere-Object { $_.SamAccountName -in $requestedUsers.sam } \n$report = foreach ($user in $filterdUsers) {\n $managerEmail = $allUsers |\n Where-Object DistinguishedName -eq $user.Manager |\n Select-Object -ExpandProperty EmailAddress\n\n [PSCustomObject][ordered]@{\n DisplayName = $user.DisplayName\n EmailAddress = $user.EmailAddress\n ManagerEmail = $managerEmail\n }\n}\n\n$report | export-csv c:\\temp\\output.csv -Notypeinformation\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2025-05-08T10:07:00.850Z","url":"https://community.spiceworks.com/t/fetch-managers-email-address/1203741/1","author":{"@type":"Person","name":"Glenn-Maxwell","url":"https://community.spiceworks.com/u/Glenn-Maxwell"}}]}}