Having trouble importing users to AD from a csv. The csv contains: firstname, lastname, password, ou

We will say the record is: test, user1, password, All Users

Here’s the script (redacted/changed/ya know):

Import-Module ActiveDirectory 
$Users = Import-Csv -Delimiter "," -Path "filepath.csv"  
foreach ($User in $Users)  
{  
    $OU = $User.OU +",DC=domain,DC=com"

    $Password = $User.password 
    $Detailedname = $User.firstname + " " + $User.lastname 
    $UserFirstname = $User.Firstname
    $FirstLetterFirstname = $UserFirstname.substring(0,1) 
    $SAM =  $FirstLetterFirstname + $User.lastname 

    New-ADUser -DisplayName $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -GivenName $user.firstname -Surname $user.lastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU  
}

Remote pssession. Script starts to run and powershell just stops. No errors. Just stops until ctrl C. Nothing popping up in any of my OUs. What am I missing here?

5 Spice ups

Not sure why that’s not working the code looks good, maybe try enclosing the path in “quotes” and I have seen in some scripts where the $true can cause an issue maybe switch that to a 1 it means the same thing…

2 Spice ups

Also sorry not to ask a dumb question but you are running this as an administrator and with an account that has rights to create users in the OU’s they’re going in? Have you also tried just running your command line without the variables with one user just as a test?

Yes, as admin. And yes, ran the cmdlets alone and was able to create a new user in the designated OU. I’m not sure where you mean on quoting the path - the variable definition is in quotes. If you mean where I call it, the rest of the variables aren’t quoted so I’m not sure why that would be the issue. But I will try it and using 1 vs. $true.

  • Is $Users populated?

  • Comment out the New-ADUser and return each of the variables in the ForEach loop. Are they populated?

  • Convert the password outside the New-ADUser with the rest of the variables, then uncomment the line and call for that variable in the command.

One thing I noticed was with this line.

$OU = $User.OU +",DC=domain,DC=com"

Using your sample data, this would yield an OU string of

All Users,DC=domain,DC=com

Switching it to this should yield a proper string.

$OU = "OU=" + $User.OU + ",DC=domain,DC=com"

Other than that, I don’t notice any problems with the script, though I can’t test it right now.

3 Spice ups

Good catch. Same result unfortunately.

1 Spice up

It’s got to be something during the import or I’m calling an incorrect variable. Tested cmdlets alone. Everything that is used in new-aduser is working correctly when done outside of the script. Changed this up for the example and attached.

Edit: okay apparently attaching doesn’t work right now.

A1 firstname B1 lastname C1 password D1 OU

A2 Test B2 User C2 password D2 All users

etc.

Spiceworks has a bug with CSV attachments that causes them to 404 when accessed. You’ll need to re-upload it as a TXT file if you want us to take a look at it.

1 Spice up

Usersexample1_-_Copy.txt (93 Bytes)

If you run this

Get-ADOrganizationalUnit -Filter 'Name -like "*"' | FT Name, DistinguishedName -A

does the format of the “All Users” OU match the format of the $OU variable?

2 Spice ups

It does.

In the file you posted it was tab delimited, in your code you put the delimiter as a comma.

4 Spice ups

Thanks Chris. That takes care of the stopping issue. I started with a text and switched to a spreadsheet when I thought it would be easier to add and remove. Missed that.

Of course fixing one issue brings up another and now I’m getting several errors of null-values. Think I will work on some other stuff and start from scratch when I’m fresh.

1 Spice up