Need to find out users manager name

Welcome Forums General PowerShell Q&A Need to find out users manager name

This topic contains 6 replies, has 5 voices, and was last updated by

Ron
 
Participant
1 year, 3 months ago.

  • Author
    Posts
  • #84994

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 240
    Helping Hand
    Rank: Participant

    what code have you written so far?

  • #85006

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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
      Points: 2
      Rank: Member

      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

    Inactive
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Hi

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

    Thanks
    Raghav

The topic ‘Need to find out users manager name’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort