0

I imported 100+ new mailboxes using Import-CSV and New-Mailbox and the Name and DisplayName fields have been populated correctly however seems I missed setting the FirstName and LastName fields.

The Name fields are in the format John Doe (every imported user only has 1 forename and 1 surname so there aren't users like John M. Doe or Le-Ann F. Blah.

I need to split the Name field and put the first word in to FirstName and last word in to LastName. How can I do this?

John
  • 1

1 Answers1

0

Because we only want to edit the same users as you imported before and not the entire AD, I'm going to use the same CSV file that your originally used. Also Because you didn't provide a sample copy of what your CSV file looks like I'm going to assume that you have the SamAccountName property in it. This should work for you.

I added the -WhatIf parameter so that you can double-check the results. Just remove it when you are ready to run it.

$CSVfile = Import-Csv C:\file.csv

Foreach ($item in $CSVfile) {
    #Find the user account.  Maybe redundant but it's the proper input to the Identity param
    $user = Get-ADUser -Identity ($item.SamAccountName) -Properties DisplayName,GivenName,Surname,UserPrincipalName

    #Split DisplayName
    $GivenName,$Surname = $user.DisplayName.split(' ',2)

    #Set GivenName and Surname
    Set-ADUser -Identity $user -GivenName $GivenName -Surname $Surname -WhatIf
}

Also in the future, please do more research before asking such a broad question. I know this question has been asked before. Example: https://social.technet.microsoft.com/Forums/en-US/c532589b-dbe1-47a2-96e1-f174348c14e0/setaduser-to-modify-givenname-and-surname-attributes-based-upon-displayname-in-powershell?forum=winserverpowershell