Nikita Levitan Nikita Levitan - 3 months ago 18
PowerShell Question

Powershell to split Display name and use it to populate First Last name

Need some help with my code. I have users in OU, they have Display Name attribute but First/Last name are empty. I am trying to populate them using powershell. here is what i got so far:

get-aduser -filter * -SearchBase 'OU=FTE,OU=GLS,OU=Staff,DC=domain,DC=com’ | % {$_.name –split " "}


This gives me the output for John Doe as

John
Doe

now i am trying to set these values using this, but it fails:

$SplitName = $_.name –split " "

get-aduser -filter * -SearchBase 'OU=FTE,OU=GLS,OU=Staff,DC=domain,DC=com’ | % {Set-ADUser -Identity $_ -GivenName $SplitName[0] -Surname $SplitName[1]}


I think i am not using the split correctly, but I am not sure.

Answer

Without AD with me, I don't see why this wouldn't work:

get-aduser -filter * -SearchBase 'OU=FTE,OU=GLS,OU=Staff,DC=domain,DC=com’ | % {
       $splitName = $_.name –split " "
       Set-ADUser -Identity $_ -GivenName $SplitName[0] -Surname $SplitName[1]
    }
Comments