Solved

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

Posted on 2014-12-03
7
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 81

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mailbox Delegation List 45 122
Basic to Dynamic disk 4 62
Script for automatic service restart 6 42
active directory 3 22
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

734 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