0

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?

CC BY-SA 3.0
7
  • Your subexpression $() is entirely unnecessary. Nov 9, 2017 at 19:37
  • changed ; to , and these changes, still not working 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 } Nov 9, 2017 at 19:41
  • added -delimiter "," not worked Nov 9, 2017 at 19:42
  • getting this as return: Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again. [ERROR] At C:\Users\r013462\source\repos\rename-project\rename-project\Script.ps1:14 char:23 [ERROR] + Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName ... Nov 9, 2017 at 19:43
  • Missing character capitalized. samaccoutname,Name,givenname,surname -> samaccouNtname,Name,givenname,surname
    – BenH
    Nov 9, 2017 at 19:51

1 Answer 1

1

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
    }

}
CC BY-SA 3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.