Add Users to AD from CSV via PowerShell

There might be a situation where you need to add users from a CSV file. In this specific example, we were adding users from a company that was acquired. NOTE: There was no desire to migrate the users via ADMT or to create a forest trust.

Before we go any further, let me do a bit of housekeeping in an effort to avoid confusion.

  • Purchasing Company = CompanyA (FDQN = companya.com)
  • Acquired Company = CompanyB (FDQN = companyb.com)

In the domain of CompanyB, we ran CSVDE to get output for all user objects from Active Directory (AD) using this command in an elevated Command Prompt or PowerShell console…

csvde.exe -r objectClass=user -f "C:\Csvde-Output.csv"

 

In the domain for CompanyA (where we were adding the users), we copied the CSV file to the C drive of one of the domain controllers.

Next we added OUs to add the new users in. We called the top-level OU “CompanyB” and created a sub-OU named “Users”. So, the distinguishedName of the OU in CompanyA’s AD looked something like this … “OU=Users,OU=CompanyB,DC=companya,DC=com”.

Then, we added a UPN suffix that will be used to create the UPN for the new users. We called it “company.com”.

Lastly, we ran the following command (using the CSV file) in an elevated PowerShell console to create the new users in the OU we created while assigning the UPN suffix we created.

Import-Csv "C:\Csvde-Output.csv" | ForEach-Object{ $Domain = "@companyb.com"; $UPN = $_.sAMAccountName+$Domain; New-ADUser -SamAccountName $_.sAMAccountName -UserPrincipalName $UPN -Name $_.Name -DisplayName $_.displayName -GivenName $_.givenName -Initials $_.initials -Surname $_.sn -Department $_.Department -Company $_.Company -Fax $_.FacsimileTelephoneNumber -City $_.l -State $_.St -PostalCode $_.PostalCode -Description $_.Description -Title $_.Title -Path "OU=Users,OU=CompanyB,DC=companya,DC=com" -AccountPassword (ConvertTo-SecureString "Password1" -AsPlainText -Force) -Enabled $True -PasswordNeverExpires $True -PassThru }

 

Let me break down the command a bit…

This assigns the value of the UPN suffix (prepended with ‘@’) that we created specifically for the new users we were adding to the variable of $Domain.

$Domain = “@companyb.com”

 

Here, user names found in the “sAMAccountName” column in the “C:\Csvde-Output.csv” file are appended to the value set for $Domain to variable for $UPN. Therefore, if one of the user names found in the sAMAccountName column is “John”, then the value for $UPN will be “John@companyb.com”.

$UPN = $_.sAMAccountName+$Domain

 

This portion of the command creates new users in the “CompanyB\Users OU” with the attributes found in the associated columns from the “C:\Csvde-Output.csv” file and sets the UserPrincipalName based on the value of the $UPN variable.

New-ADUser -SamAccountName $_.sAMAccountName -UserPrincipalName $UPN -Name $_.Name -DisplayName $_.displayName -GivenName $_.givenName -Initials $_.initials -Surname $_.sn -Department $_.Department -Company $_.Company -Fax $_.FacsimileTelephoneNumber -City $_.l -State $_.St -PostalCode $_.PostalCode -Description $_.Description -Title $_.Title -Path “OU=Users,OU=CompanyB,DC=companya,DC=com”

 

Here we set the password to “Password1” and set it to never expire, enabled the account (default new users are disabled), and used the PassThru parameter displays confirmation the user is created.

-AccountPassword (ConvertTo-SecureString “Password1” -AsPlainText -Force) -Enabled $True -PasswordNeverExpires $True -PassThru

 

When we put it all together, encapsulated in squiggly brackets, using the “Import-Csv” command with “ForEach-Object”, the “C:\Csvde-Output.csv” file is traversed row by row to create new users with the attributes and values we needed to complete the project successfully.

Import-Csv “C:\Csvde-Output.csv” | ForEach-Object{ … }

 

Reference(s):

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s