Rick's Tech Gab

A little Gab about this and that, but mainly Technology!!

PowerShell Script Create user from CSV

leave a comment »

During testing you always need to create user accounts. But who wants to spend a whole bunch of time creating users? Not me! I went ahead and made a very basic PowerShell Script that will create users from a CSV file.

This is a very Basic Script, it will set a password on the account, allow you to specify the OU to place the accounts, populate the GivenName, Surname, Displayname, Userprincipalname, Samaccountname and enable the account. It can be tweaked to do more. But for me at this moment it works quiet well.

You need to create a CSV file, called latestusers.csv, the file will be in this format

“name,GivenName,Surname,userprincipalname,samaccountname”

image

The Script data is below, copy the below text into Notepad, and save it as CreateUser.ps1

$password = Read-host “Please Enter your password” -AsSecureString
$path = Read-host “Please Enter your OU”
Import-Csv latestusers.csv | foreach {New-ADUser -Name $_.name -GivenName $_.GivenName -Surname $_.Surname -DisplayName $_.Name -UserPrincipalName $_.userprincipalname -SamAccountName $_.SamAccountName -AccountPassword $password -Enabled $true -path $path}

The text highlighted in Red is all one command and should be on the same line, whereas the Black is meant to be on separate lines.

To run the script:

  • Run PowerShell as Administrator
  • Change to the location where you have placed the Script and the CSV file. For me it will be C:\ “Set-Location c:\”
  • Now run the script. Tyoe “.\createuser.ps1” if you get an error trying to run the script like the screen shot below. Run this Command “Set-ExecutionPolicy Remotesigned”, this will allow you to run scripts.

image,

  • You will get asked to enter a password, this password will be set on all accounts. Enter in a password that meets the requirements of your Domain.
  • You will the get prompted to Enter your OU, in my case this will be “OU=Accounts,DC=Adatum,DC=Com” Once you press enter the Script will then go look in your CSV file, and create the accounts with the data you have populated in the file.

image

image

  • Again this is a very Basic script, but it can be tweaked to add more data to the user account, but works very good for creating a bunch of test user that are active. Plus beats the hell out of the GUI, and the amount of mouse work you would have to do.
Advertisements

Written by rickrbyrne

June 23, 2011 at 9:03 pm

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

%d bloggers like this: