Need to find out users manager name

This topic contains 6 replies, has 5 voices, and was last updated by  Ron 1 week ago.

  • Author
    Posts
  • #84994

    raghavendra
    Participant

    Hi

    I have a Csv file in below format.

    some-id userid
    1 test1
    2 test2
    3 test3
    4 test4
    5 test5

    I want to select "userid" column and i need to find out the manager for each user, and append csv in 3rd column with user's manager name. How to achieve this task. Please advise

    Thanks in advace
    Raghav

  • #84997

    Sam Boutros
    Participant

    what code have you written so far?

  • #85006

    Richard Siddaway
    Moderator

    This is the basic code you need

    Import-Csv -Path .\names.csv |
    foreach {
       $user = Get-ADUser -Identity $_.userid -Properties Manager
       $_ | Add-Member -MemberType NoteProperty -Name 'Manager' -Value $user.Manager
       $_
    }
    

    The manager is given as a distinguished name. If you want the managers name then modify the code like this

    Import-Csv -Path .\names.csv |
    foreach {
       $user = Get-ADUser -Identity $_.userid -Properties Manager
       $manager = Get-ADUser -Identity $user.Manager
       
       $_ | Add-Member -MemberType NoteProperty -Name 'Manager' -Value $manager.Name
       $_
    }
    

    if you want the data written back to a csv file then use Export-Csv

    Import-Csv -Path .\names.csv |
    foreach {
       $user = Get-ADUser -Identity $_.userid -Properties Manager
       $manager = Get-ADUser -Identity $user.Manager
       
       $_ | Add-Member -MemberType NoteProperty -Name 'Manager' -Value $manager.Name
       $_
    } | Export-Csv -Path names2.csv -NoTypeInformation
    
    • #87476

      raghavendra
      Participant

      Hi

      I used your script and it worked. When I run this script it is giving errors when the AD user not found in AD. How to capture the deleted users in the same script.

      Thanks in advance
      Raghav

    • #87487

      Ron
      Participant

      Untested, but something like this:

      Import-Csv -Path .\names.csv |
      foreach {
         try {
            $user = Get-ADUser -Identity $_.userid -Properties Manager
            $manager = Get-ADUser -Identity $user.Manager | select -expand Name
         } catch {
            $manager = "Invalid User ID or Manager"
         }
      
         $_ | Add-Member -MemberType NoteProperty -Name 'Manager' -Value $manager
         $_
      
      } | Export-Csv -Path names2.csv -NoTypeInformation
  • #85042

    edwin arlington
    Participant

    You can try the following command for manager information

    Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } `
    -SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" `
    -Properties objectGUID,displayName,office,division,department,employeeNumber,
    employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail,
    @{Label="Manager";Expression={(Get-aduser -filter {sAMAccountName -eq $_.Manager}.sAMAaccountName)}},
    sAMAccountName |
    Export-CSV "EU_AD_Properties.csv"

    or

    (get-aduser (get-aduser $user -Properties manager).manager).samaccountName

  • #85304

    raghavendra
    Participant

    Hi

    Apologies for the delayed reply. Thanks a lot the script worked for me.

    Thanks
    Raghav

You must be logged in to reply to this topic.