Avatar of Sir Learnalot
Sir Learnalot
 asked on

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

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!
PowershellWindows Server 2012Active Directory

Avatar of undefined
Last Comment
Sir Learnalot

8/22/2022 - Mon
SOLUTION
sirbounty

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Sir Learnalot

ASKER
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

Rajitha Chimmani

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.
ASKER CERTIFIED SOLUTION
David Johnson, CD

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Sir Learnalot

ASKER
@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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Rajitha Chimmani

Sometimes..yes..it does help. but, if you have a single DC then it does not matter.
Sir Learnalot

ASKER
did not end up solving the issue, will recreate a question later