Please help: I want to create 600 users in 35 different Ous at once in one PowerShell script.
Jkarkor,
Welcome to the forum.
What kind of help do you expect?
This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.
We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.
This is my code but its not working
I want to use one foreach loop to create the OU and the Users at once from the same csv file
#Enter a path to your import CSV file
$ADUsers = Import-csv C:\temp\ounusers.csv
foreach ($User,$ou in $ADUsers)
{
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$department = $User.department
$name = $ou.department
$path =$ou.path
$email = $User.email
$streetaddress = $User.streetaddress
$zipcode = $User.zipcode
$country = $User.country
$telephone = $User.telephone
$state = $User.state
$ciy = $User.city
$jobtitle = $User.jobtitle
$company = $User.company
$description = $User.description
$OU = $User.ou
#firstname,middleInitial,lastname,username,email,department,password,telephone,jobtitle,company,ou
#Check if the user account already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, output a warning message
Write-Warning "A user account $Username has already exist in Active Directory."
}
else
{
#If a user does not exist then create a new user account
#Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@kartech.com" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-ChangePasswordAtLogon $True `
-DisplayName "$Lastname, $Firstname" `
-Department $Department`
-Name $Department`
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}
}
After running the above code, here is the error message
At line:4 char:15
- foreach ($User,$ou in $ADUsers)
-
~
Missing ‘in’ after variable in foreach loop.
At line:4 char:20
- foreach ($User,$ou in $ADUsers)
-
~~
Unexpected token ‘in’ in expression or statement.
At line:4 char:31
- foreach ($User,$ou in $ADUsers)
-
~
Unexpected token ‘)’ in expression or statement.
+ CategoryInfo : ParserError: ( , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingInInForeach
While you can use the same data source for OUs and Users I’d rather do the creation process in two separate loops.
Since the cmdlet New-ADUser
does not create an OU if it does not exist you have to create the OUs before you create a new user in it. I’d extract all needed OUs from the CSV and create them before I loop over all users to create their accounts.
If you insist on using one loop only you have to make sure for each individual user you want to create that the according OU already exists.
That does not make sense … could you share some sample lines of your CSV file (sanitized of sensitive information and formatted as code please)?
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance
How to format code in PowerShell.org 1 <---- Click
( !! Sometimes the preformatted text button hides behind the settings gear symbol. )
Thanks a Million in advance. But please, how do I go about the creation process in two separate loops.?
Thanks for your help.
I will quote myself …
It does not have to be the complete CSV … only to see the basic properties …
firstname,middleInitial,lastname,username,email,department,password,ou
Garrison,S,Flahn,gflahn,gsflahn@test.com, Facilities Management ,P@SSw1rd,“OU=HomeWord,DC=kartech,DC=com”
Abiodun,A,Gbalah,agbalah,agbalah@test.com,Business College ,P@SSw1rd,“OU=HomeWord,DC=kartech,DC=com”
firstname,middleInitial,lastname,username,email,department,password,ou
Garrison,S,Flahn,gflahn,gsflahn@test.com, Facilities Management ,P@SSw1rd,“OU=HomeWord,DC=test,DC=com”
Abiodun,A,Gbalah,agbalah,agbalah@test.com,Business College ,P@SSw1rd,“OU=HomeWord,DC=test,DC=com”
Your sample data contains exactly 1 (one) OU …
And please format it as code.
firstname, middleInitial, lastname, username, department, password,ou
I would like to make each department name to be the need for the respective Ou and nested sub-Ou for Users that will host the users
John , M,King ,jmking,jmking@test.com, Facilities Management ,Password!1,“OU=HomeWord,DC=test,DC=com”
Olaf, by now, I believe you already know that I am not very knowledgeable in Powershall like you. I wish the concept explained can give you a clue to my problem.
Yes, it does.
Your sample data are still a bad example since there are only 2 users and 2 OUs.
Anyway … I added 2 more sample users to make it more real world and show how it works …
Input data:
$CSVInput = @'
firstname,middleInitial,lastname,username,email,department,password,ou
Garrison,S,Flahn,gflahn,gsflahn@test.com, Facilities Management ,P@SSw1rd,"OU=HomeWord,DC=test,DC=com"
Abiodun,A,Gbalah,agbalah,agbalah@test.com,Business College ,P@SSw1rd,"OU=HomeWord,DC=test,DC=com"
John,,Lennon,jlennon,jlennon@test.com, Facilities Management ,P@SSw1rd,"OU=HomeWord,DC=test,DC=com"
George,,Harrison,gharrison,gharrison@test.com,Business College ,P@SSw1rd,"OU=HomeWord,DC=test,DC=com"
'@ |
ConvertFrom-Csv
… looks like this:
PS:>$CSVInput |
Format-Table -AutoSize
firstname middleInitial lastname username email department password ou
--------- ------------- -------- -------- ----- ---------- -------- --
Garrison S Flahn gflahn gsflahn@test.com Facilities Management P@SSw1rd OU=HomeWord,DC=test,DC=com
Abiodun A Gbalah agbalah agbalah@test.com Business College P@SSw1rd OU=HomeWord,DC=test,DC=com
John Lennon jlennon jlennon@test.com Facilities Management P@SSw1rd OU=HomeWord,DC=test,DC=com
George Harrison gharrison gharrison@test.com Business College P@SSw1rd OU=HomeWord,DC=test,DC=com
Creating the list of unique departments:
$DepartmentList =
$CSVInput |
Select-Object -Property ou, department -Unique
… looks like this:
PS:>$DepartmentList
ou department
-- ----------
OU=HomeWord,DC=test,DC=com Facilities Management
OU=HomeWord,DC=test,DC=com Business College
Creating the OUs:
foreach ($Department in $DepartmentList) {
New-ADOrganizationalUnit -Name $Department.department -Path $Department.ou
}
Creating new user:
foreach ($Item in $CSVInput) {
$NewADUserSplat = @{
SamAccountName = $Item.username
UserPrincipalName = '{}@kartech.com' -f $Item.username
Name = '{0} {1}' -f $Item.firstname, $Item.lastname
GivenName = $Item.firstname
Surname = $Item.lastname
Enabled = $True
ChangePasswordAtLogon = $True
DisplayName = '{1}, {0}' -f $Item.firstname, $Item.lastname
Department = $Item.Department
Path = 'OU={0},{1}' -f $Item.department, $Item.ou
}
New-ADUser @NewADUserSplat
}
!! Untested !!
Please test this in a test environment!!
Read about …
Thanks. but do I have to run to separate CSV file?
Did I use separate CSV inputs?
Run the code line by line and see what it does. Replace the $CSVInput
with the imported data from a test CSV and see what the output of the variables look like …
Use test data and try!! This way you learn faster than asking every little step in a forum and waiting for some replies.