Solved

Powershell script to create user names from a text file - HELP!

Posted on 2014-12-03
7
428 Views
Last Modified: 2014-12-22
Hello Experts,
I need help creating a script in PowerShell to create users in AD from a csv file. The file contains users in this format:

COMPANY,SAM-Account Name, Password, Path (OU to be placed in), First Name, Initial, Last Name, Display Name, Security Group, CannotChangePassword, PasswordNeverExpires, email address, user-principle-name

So as a sample user whos name is Joe Blow and works at EE, this is how the format is in the csv:

EE,jblow,TempPass1,"OU=EE,OU=Customers,DC=Portal,DC=local",Joe,J,Blow, Joe Blow,PORTAL.local\Customers,FALSE,TRUE,joe.blow@expertsexchange.com,jblow@portal.local

The users I am importing from the CSV file can be exported in another format or rearranged if it helps, thanks in advance!
0
Comment
Question by:Sir Learnalot
7 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 250 total points
ID: 40479228
Does the csv have column headers?  If so, you might use import-csv to bring in the data.
Otherwise, if you have to 'force' it, this should get the job done: (confirm is set, to prevent it from completing without your say-so)
$data = get-content 'filename.csv'
foreach ($values in $data) {
  $item = $values.split(',')
  $COMPANY=$item[0]
  $SAM = $item[1]
  $Password = $item[2]
  $OUPath=$item[3]
  $FirstName = $item[4]
  $Initial=$item[5]
  $LastName=$item[6]
  $DisplayName=$item[7]
  $SecurityGroup=$item[8]
  $CannotChangePassword=$item[9]
  $PasswordNeverExpires=$item[10]
  $emailaddress=$item[11]
  $upn=$item[12]
  New-ADUser -samaccountname $SAM -company $company -DisplayName $displayName -EmailAddress $emailaddress -givenname $firstname -sn $lastname -initials $initial -passwordneverexpires $passwordneverexpires -userprincipalname $upn -path $OUPath -CannotChangePassword $CannotChangePassword -confirm $true
add-adgroupmember $securitygroup $samaccountname
}

Open in new window

0
 
LVL 6

Author Comment

by:Sir Learnalot
ID: 40479589
I tried this and ran into tons of trouble :P I do appreciate the effort though as I was able to use your code with some slight modifications :) However, now that I got the syntax properly, I am running into another error...

screen of ps error
$data = get-content 'C:\Users\portalservice\Desktop\Test1.csv'
$Password = (Read-Host -AsSecureString "AccountPassword")
foreach ($values in $data) {
  $item = $values.split(':')
  $COMPANY=$item[0]
  $SAM = $item[1]
  $Pword = $item[2]
  $OUPath=$item[3]
  $FirstName = $item[4]
  $Initial=$item[5]
  $DisplayName=$item[6]
  $SecurityGroup=$item[7]
  $LastName=$item[8]
  $emailaddress=$item[9]
  $upn=$item[10]
  New-ADUser -Name $DisplayName -samaccountname $SAM -AccountPassword $Password -company $COMPANY -DisplayName $DisplayName -EmailAddress $emailaddress -givenname $FirstName -Surname $LastName -initials $Initial -PasswordNeverExpires 1 -userprincipalname $upn -path $OUPath -CannotChangePassword 0 -confirm
Add-adgroupmember $SecurityGroup $SAM
}

Open in new window

0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40479628
I guess you need to give some time after creating the user before you add that user to a group

Try the command, start-sleep -s 30 between new-aduser and add-adgroupmember

Also, you may consider specifying the -domaincontroller parameter in New-ADUser command.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 80

Accepted Solution

by:
David Johnson, CD, MVP earned 250 total points
ID: 40479647
in the first line of the csv change everything with spaces to something else i.e. underscores, hyphens
user security groups use
$user.samaccountname | Add-ADPrincipalGroupMembership [-Identity] $user.SecurityGroup
#
# untested will add more later
#
$users = import-csv -Path c:\test\Q_28573873.csv
foreach ($user in $users){
$changepassword = $true
$passwordExpires = $true
if ($user.CannotChangePassword = "FALSE") { $changepassword = $false}
if ($user.PasswordNeverExpires = "TRUE") { $passwordExpires = $true}
new-aduser -samaccountname $user.Sam-AcccountName -company $user.COMPANY -Password $user.Password -OU $user.OU -Given $user.First_Name -Initials $user.Initial -SurName $user.Last_Name -CannotChangePassword $changepassword -PasswordNeverExpires $passwordExpires
$user.samaccountname | Add-ADPrincipalGroupMembership [-Identity] $user.SecurityGroup
#$user | fl
}

Open in new window

Q-28573873.csv
0
 
LVL 6

Author Comment

by:Sir Learnalot
ID: 40482925
@Rajitha I tried removing the add to group entirely so thats not the problem at the moment. Would adding the DC matter if I am running the script directly off the server anyways?
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40482987
Sometimes..yes..it does help. but, if you have a single DC then it does not matter.
0
 
LVL 6

Author Closing Comment

by:Sir Learnalot
ID: 40512908
did not end up solving the issue, will recreate a question later
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
In this Micro Tutorial viewers will learn how they can get their files copied out from their unbootable system without need to use recovery services. As an example non-bootable Windows 2012R2 installation is used which has boot problems.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question