PowerShell

PowerShell: Import Active Directory Users from CSV

Comma separated files are widely used when it comes to bulk import or export of data. In this blog post I will provide a sample script to import Active Directory users from a csv file. Let’s jump in.

The CSV File

The following *.csv file serves as an example you can build on.

Unbenannt.PNG

As you can see, the following Active Directory user account attributes are provided:

  • Name
  • Givenname
  • E-Mail Address
  • samAccountName
  • ParentOU

Which commonly used settings are missing?

  • Password
  • User must change password at first logon
  • Accounts should be enabled

The Script

In this part I will provide the script to import the users from the example csv file and fix the missing arguments we discussed above.

These are the things you have to watch out for:

  • Take care of the delimiter. Omit the parameter if comma is used, because default is comma
  • Watch out for the file location
  • Change the AccountPassword at your choice
  • add more parameters as you see fit

If you are ready, copy it into your PowerShell ISE session and have fun.

Import-Csv "C:\temp\test.csv" -Delimiter ';' |

ForEach-Object { 
New-ADUser `
-Name $_.Name `
-GivenName $_.givenname `
-Surname $_.sn `
-Path $_."ParentOU" `
-SamAccountName $_.samAccountName `
-UserPrincipalName ($_.samAccountName + '@' + $env:userdnsdomain) `
-AccountPassword (ConvertTo-SecureString "123user!!!!!" -AsPlainText -Force) `
-EmailAddress $_."E-Mail Address" `
-Enabled $true `
-ChangePasswordAtLogon $true 
} 

A quick glance at them shows that they are enabled and ready for the first login.

Unbenannt.PNG

Cheers!

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.