Jump to content

Recommended Posts

Posted

Hello all,

 

Thought I would share this as it has taken 2 hours of mashing my head against a wall getting it working and thought I would spare others a bit of pain

 

This script uses the 'Active Directory module for Windows Powershell' but the key point (that took me ages to work out, and seems mad) is you should not run it from a domain controller! instead I ran it from a member server while logged in as a Domain Admin

 

Import-Csv .\2012Users.csv | foreach-object {
$userprinicpalname = $_.SamAccountName + "@yourdomain.com"

New-ADUser -SamAccountName $_.SamAccountName -UserPrincipalName $userprinicpalname -Name $_.name -DisplayName $_.name -GivenName $_.FirstName -SurName $_.LastName -Path "OU=year7,OU=Pupils,DC=yourdomain,DC=com" -AccountPassword (ConvertTo-SecureString "Passwordgoeshere" -AsPlainText -force) -Enabled $True -ChangePasswordAtLogon $True -PassThru }

 

obviously change the yourdomain.com, path to the OU and default password to suit your school.

 

the CSV should have the following columns

 

FirstName

LastName

name

SamAccountName

 

when run the script will create the users, using their login name as the Display name. No Profile or home folder will be set - this could be added in I think, but is straight forward enough to do in AD users and computers (and I was fed up with tinkering in powershell by that point!)

 

Hope this is useful to someone.

  • Thanks 3
  • 1 year later...
Posted (edited)

I know I'm a little late to the game here sorry, however I have updated this a little bit to be more flexible, if not readable.

 

 

$import = Import-Csv .\Users.csv
 foreach-object -input$import {
     $upn = $_.SamAccountName +"@YOURDOMAIN"
     $name = $_.name
     $displayname = $_.name
     $givenname = $_.FirstName
     $surname = $_.Lastname
     $Path = "OU=TEST,OU=USERS,OU=TEMP,DC=YOURDOMAIN,DC=com"
     $Password = "PASSWORD GOES HERE"
     $enabled = $True
     $changePW = $False
     new-ADUser -Name$name -SamAccountName $_.SamAccountName -UserPrincipalName $upn -DisplayName $displayname -GivenName $givenname -SurName $surname -Path $path -AccountPassword (ConvertTo-SecureString $password -AsPlainText -force) -Enabled $enabled -ChangePasswordAtLogon $changePW -PassThru
} 
Edited by neil1775

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...