Forum Discussion
Jord9857
Nov 01, 2021Copper Contributor
Creating Users with a CSV
Hi all, I'm trying to create a script which will pull user's info from a .csv, check the user's DisplayName with AD to make sure a user with that DisplayName doesn't already exist, does exact sam...
J Mymryk
Nov 05, 2021Copper Contributor
Hi,
You need to look at your logic a bit closer. You have IF Statements but they are not nested or
are using AND comparison to have both components match. Also you are not throwing a variable that says the user exists to provide a logic check if you actually need to create the account. Right now it runs each if statement then runs the creation statement.
I find it may help sometimes to write down in a document the logic process before tackling the coding.
You need to look at your logic a bit closer. You have IF Statements but they are not nested or
are using AND comparison to have both components match. Also you are not throwing a variable that says the user exists to provide a logic check if you actually need to create the account. Right now it runs each if statement then runs the creation statement.
I find it may help sometimes to write down in a document the logic process before tackling the coding.
Jord9857
Nov 10, 2021Copper Contributor
So what do I need to do/change please?
- J MymrykNov 18, 2021Copper Contributor
I've put some additional comments in your script but as I said before write out the logic on some paper and it will help you write the correct statements. Right now you just have a loop that applies every action to each item that goes through the script. The if statements should have an else statement that creates the list of users you are going to act against or you will just attempt to recreate all users each time. The creation portion of the script should only loop through the users who don't exist properly, not every user.
# Start Commands Write-Host "STARTED SCRIPT`r`n" # Import Active Directory Module For Running AD Cmdlets Import-Module ActiveDirectory # Store The Data From ADUsers.csv in THE $ADUsers variable $filepath = Import-Csv -Path C:\Users\Administrator\Desktop\users1.csv # Loop through each row containing user details in the csv file ForEach ($user in ($filepath)) { # Check if User(s) display name already exists $ADUser = Get-AdUser -Filter {$displayname -eq $User.DisplayName} if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') { Write-Host "$displayname already exists" -ForegroundColor Red } #What are you going to do if the user doesn't exist? (else Statement should be here to put the Incorrects into an array to correct later right?? # Check if User(s) username already exists $ADUser = Get-AdUser -Filter {$SamAccountName -eq $User.SamAccountName} if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') { Write-Host "$SamAccountName already exists" -ForegroundColor Red } #What are you going to do if the username doesn't exist? (else Statement should be here to put the Incorrects into an array to correct later right?? # Should not this section only be done for users who didn't pass the above if statement? otherwise the following commands apply to all users in the file # Read user data from each field in each row and assign the data to a variable as below $displayname = $User.'firstname' + " " + $User.'lastname' $firstname = $User.'firstname' $lastname = $User.'lastname' $jobtitle = $User.'jobtitle' $telephone = $User.'telephone' $department = $User.'department' $UPN = $User.'email' $SamAccountName = $User.'username' $EmailAddress = $User.'email' $Password = $User.'password' $OU = $User.'OU' # Create New AD Users New-ADUser -Name "$displayname" -DisplayName "$displayname" -GivenName "$firstname" -Surname "$lastname" -Title "$jobtitle" -OfficePhone "$telephone" -Department "$department" -UserPrincipalName "$UPN" -SamAccountName "$SamAccountName" -EmailAddress "$EmailAddress" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $true -PasswordNeverExpires $false # Confirm Commands $? | out-null $ADUser = Get-ADUser -Filter {DisplayName -eq $displayname} if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$displayname account was successfully created" -ForegroundColor Green} else {Write-Host "$displayname account was unsuccessfully created" -ForegroundColor Red} $ErrorActionPreference = 'SilentlyContinue' } # Finish Commands Write-Host "FINISHED SCRIPT`r`n" - AharonBensadounNov 10, 2021Copper ContributorHi Jord9857,
Are you sur your csv is comma delimited ? Because I tested your script and it's working fine for me.- Jord9857Nov 10, 2021Copper Contributor
Yep, tried both CSV (Comma delimited) and CSV UTF-8 (Comma delimited) but still same issue 😕