Nikita Levitan
Nikita Levitan

Reputation: 15

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.

Upvotes: 1

Views: 1823

Answers (2)

irThemis
irThemis

Reputation: 46

You can always use a range as well can combine the mid last into one last name. like John Van Smith would have $var[1..-1] # VanSmith.

PS> Get-ADUser -SearchBase 'OU=FTE,OU=GLS,OU=Staff,DC=domain,DC=com' -Filter * | % {
     $dnSplit = $_.Name -split " "; Set-ADUser -Identity $_ -GivenName $dnSplit[0] -Surname $($dnSplit[1..-1] -join "")
}

Upvotes: 0

Austin T French
Austin T French

Reputation: 5131

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]
    }

Upvotes: 1

Related Questions