Modify Manager in AD from CSV with plain names (displayname)

Welcome Forums General PowerShell Q&A Modify Manager in AD from CSV with plain names (displayname)

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

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #108254

    Participant
    Points: 0
    Rank: Member

    Hello everyone,

    I am trying to modify the Manager attribute from a CSV file I was given that only has the users "First Last" and the managers "First Last" names in it. I am trying to figure out what the best way to do this is, should I use a separate script to get those names into a CSV that has the samAccountName and DistinguishedName and then just use a simple script to bring that in or is there a way I can modify this script I found from kunaludapi on Github?

    https://github.com/kunaludapi/powershell-change-aduser-manager/blob/master/Update-ADUserManger.ps1

     

    Thank you

  • #108257

    Participant
    Points: 43
    Rank: Member

    Hi Llamllam,

    Are you trying to pull the data from AD into a report or are you trying to update the attribute in AD using the data from the report you have? if you want to update AD from the CSV file then this script should do the trick for you. You would run it like this,

    Import-Csv -Path X.csv | foreach-object{Update-ADManager -UserFullName $_.UserColumnHeader -ManagerFullName $_.ManagerColumnHeader -Whatif}
    Replace UserColumnHeader and ManagerColumnHeader with whatever the headers are from your CSV
    The Whatif is there to stop it making changes until you are happy with what it is changing.

    function Update-ADManager {
        [cmdletbinding(supportsshouldprocess=$true,ConfirmImpact='Medium')]
        Param (
            [Parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
            [string[]]$UserFullName,
    
            [parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
            [string[]]$ManagerFullName
        )
    Begin {
        if ((Get-Module ActiveDirectory) -eq $False) {
            Import-Module ActiveDirectory
        }
    }
    Process {
        $UserDetails = Get-Aduser -Filter {Name -like $UserFullName}
        $ManagerDetails = Get-Aduser -Filter {Name -like $ManagerFullName}
        if (($UserDetails.count) -gt 1) {
            Throw "Multiple users found for $UserFullName"
            
        }
        if (($ManagerDetails.count) -gt 1) {
            Throw "Multiple users found with $ManagerFullName"
            
        }
    
        if ($PSCmdlet.ShouldProcess($UserFullName,"Set-Aduser -Manager $ManagerFullName")) {
            Try {
                Set-ADuser -Identity $UserDetails.SamaccountName -Manager $ManagerDetails.SamaccountName
            }
            Catch {
                Write-Error "Unable to set AD manager for $UserFullName"
            }
        }
    
    }
    End {
    
    }
    }
    
  • #108259

    Participant
    Points: 43
    Rank: Member

    Hi Llamllam,
    This should accomplish what you want, it will accept input from a csv via the pipeline. You would use it like this,

    Import-CSV -Path X.csv | Foreach-Object {Update-AdManager -UserFullName $_.UserColumnHeader -ManagerFullName $_.ManagerColumnHeader -WhatIf}
    
    function Update-ADManager {
        [cmdletbinding(supportsshouldprocess=$true,ConfirmImpact='Medium')]
        Param (
            [Parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
            [string[]]$UserFullName,
    
            [parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
            [string[]]$ManagerFullName
        )
    Begin {
        if ((Get-Module ActiveDirectory) -eq $False) {
            Import-Module ActiveDirectory
        }
    }
    Process {
        Foreach ($User in $UserFullName) {
            $UserDetails = Get-Aduser -Filter {Name -like $User}
            $ManagerDetails = Get-Aduser -Filter {Name -like $ManagerFullName}
            if (($UserDetails.count) -gt 1) {
                Throw "Multiple users found for $UserFullName"
                
            }
            if (($ManagerDetails.count) -gt 1) {
                Throw "Multiple users found with $ManagerFullName"
                
            }
    
            if ($PSCmdlet.ShouldProcess($UserFullName,"Set-Aduser -Manager $ManagerFullName")) {
                Try {
                    Set-ADuser -Identity $UserDetails.SamaccountName -Manager $ManagerDetails.SamaccountName
                }
                Catch {
                    Write-Error "Unable to set AD manager for $UserFullName"
                }
            }
        }   
    }
    End {
    
    }
    }
    

    The -Whatif is there so you can confirm it works as expected before allowing it to actually update the manager of the user. Replace the UserColumnHeader and ManagerColumnHeader with the header names from your CSV

    • #108271

      Participant
      Points: 0
      Rank: Member

      Johnathan,

      Wow thank you so much,  this is working perfectly for me.

  • #108274

    Participant
    Points: 160
    Helping Hand
    Rank: Participant

    An important part of this is how the names are formatted in AD. If the users are formatted Last, First MiddleInit, then you need to reformat the names and the LIKE doesn't have any wildcards. If the First and Last name is provided, you should search those attributes in Active Directory. Here is a modified version of Jonathon's function:

    function Set-ADManager {
        [cmdletbinding(supportsshouldprocess=$true,ConfirmImpact='Medium')]
        Param (
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [string]$UserFirstName,
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [string]$UserLastName,
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [string]$ManagerFirstName,
            [parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [string]$ManagerLastName
        )
        Begin {
            if ((Get-Module ActiveDirectory) -eq $False) {
                Import-Module ActiveDirectory
            }
        }
        process {
            Write-Verbose ("Searching for user {0} {1}" -f $UserFirstName, $UserLastName)
            $UserDetails = Get-Aduser -Filter {(GivenName -eq $UserFirstName) -and (SurName -eq $UserLastName)}
                
            if (@($UserDetails).Count -eq 1) {
                Write-Verbose ("Searching for manager {0} {1}" -f $ManagerFirstName, $ManagerLastName)
    
                $ManagerDetails = Get-Aduser -Filter {(GivenName -eq $ManagerFirstName) -and (SurName -eq $ManagerLastName)}
    
                if (@($ManagerDetails).Count -eq 1) {
                
                    if ($PSCmdlet.ShouldProcess(('{0} ({1})' -f $UserDetails.Name, $UserDetails.SamAccountName) ,('Set-Aduser -Manager {0} ({1})' -f $ManagerDetails.Name, $ManagerDetails.SamAccountName))) {
                        try {
                            $params = @{
                                Identity    = $UserDetails
                                Manager     = $ManagerDetails
                                ErrorAction = "Stop"
                            }
                            
                            Set-ADuser @params
                        }
                        catch {
                            $msg = 'Error setting manager for user {0} {1}. {2}' -f $UserFirstName, $UserLastName, $_
    
                            Throw $msg
                        }
                    }
                }
                else {
                    $msg = 'Error finding user {0} {1}, found {2} Active Directory records' -f $ManagerFirstName, $ManagerLastName, @($ManagerDetails).Count
                    Throw $msg
                }
            }
            else {
                $msg = 'Error finding user {0} {1}, found {2} Active Directory records' -f $UserFirstName, $UserLastName, @($UserDetails).Count
                Throw $msg
            }
            
        }
        end {
    
        }
    }
    
    
    $csv = Import-CSV C:\Scripts\Managers.csv
    
    $csv | Set-ADManager -Verbose -WhatIf
    

    Keep in mind, this assumes your CSV column headers are UserFirstName, UserLastName, ManagerFirstName, ManagerLastName. If they are not, you can change the header with Import-CSV

    Import-CSV C:\Scripts\Manager.csv -Header UserFirstName, UserLastName, ManagerFirstName, ManagerLastName
    
  • #108284

    Participant
    Points: 0
    Rank: Member

    Rob, you are the man.  Worked perfectly!

    Thank you so much for all your help!

The topic ‘Modify Manager in AD from CSV with plain names (displayname)’ is closed to new replies.