Powershell - rename users (loop)

1.5k Views Asked by At

I'm trying to update a few (560) users on my domain. they are using incomplete and/or incorrect names according to my workers DB.

I created a CSV file containing this info:

samaccoutname,Name,givenname,surname
r001248,ADRIANA DAS COUVE ,ADRIANA ,DAS COUVE
r020230,ALEXANDRA DAS NEVE ,ALEXANDRA ,DAS NEVE

This is my code but it isn't working out:

#
# Script.ps1
#

Import-Module activedirectory

$userlist = Import-Csv C:\Users\r013462\Documents\Atualização_AD.csv -Delimiter ","

foreach ($user in $userlist)
{
    $GivenN = $user.givenName
    $FullN = $user.Name
    $SurN = $user.surName
    Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName $GivenN -Surname $SurN -DisplayName $FullN
}

suggestions?

1

There are 1 best solutions below

0
Sidney Morais On

Ok, finally, came to a solution!

if you want to do it, you have to use same parameters of my csv orginally posted and use this script:

#
# Script.ps1
#

Import-Module activedirectory

$varCSV = ""
$userlist = Import-Csv -Path $varCSV -Delimiter ","

foreach ($user in $userlist)
{
    $samN = $user.samaccouNtname
    $GivenN = $user.GivenName
    $FullN = $user.Name
    $SurN = $user.Surname
    $dn = (Get-ADUser -Identity $samN).DistinguishedName
    Get-ADUser -Identity $user.SamAccountName | Set-ADUser -GivenName $GivenN -SurName $SurN -DisplayName $FullN  
    Try {
        Rename-ADObject $dn -NewName $FullN
    }

    catch {
        Write-Output "usuario repetido: " ($user.samaccountname) | Out-File C:\errors.txt -Append
    }

}