0
votes

I'm trying to import users into AD, using a PowerShell script, and a CSV.

Here's my script (with names removed):

$pass = ConvertTo-SecureString "passwordhere" -AsPlainText -Force

Import-Csv .\adimport.csv | New-ADUser -AccountPassword $pass -Enabled $true -ChangePasswordAtLogon $true

Get-ADUser -Filter * -SearchBase 'OU=<ouname>,OU=<ouname>,OU=<ouname>,OU=<ouname>,DC=<name>,DC=<name>,DC=<name>,DC=<name>' -Properties userPrincipalName | foreach { Set-ADUser $_ -UserPrincipalName "$($_.samaccountname)@<domainname>"}

And here's how my CSV looks:

Name    Username    SamAccountName  GivenName   Surname DisplayName Description Path    Enabled Postalcode  StreetAddress   City    Title   Office  Company extensionAttribute2 officephone

Everything works fine, the users are imported (into the correct OU), and all the information is updated, apart from extensionAttribute2.

Does anyone have know what I would need to change to get extensionAttribute2 in AD to import the information under extensionAttribute2, from the CSV, when running the script?

If anyone needs any more information, let me know.

Any help would be appreciated. Thanks.

1

1 Answers

0
votes

To set an Extension Attribute, use this syntax:

 Set-ADUser –Identity $ThisUser -add @{"extensionattribute1"="MyString"}

You can also specify other attributes using the -OtherAttributes param for New-ADUser

So, integrating this into your code, I'm attempting to create the new user and also specify the Extension Attribute at the same time.

Import-Csv .\adimport.csv | New-ADUser -AccountPassword $pass -Enabled $true `
  -ChangePasswordAtLogon $true `
  -OtherAttributes  @{"extensionattribute2"=$_.extensionAttribute2}

Lemme know if this helps :)