0

I need to create a lot of users so I wanted to use this script, I created it by myself. The script completed, but if I look in my ADUC there are no new users, what is wrong with my script?

Import-Module ActiveDirectory
$ADusers = Import-csv C:\TEST\Create.CSV

Foreach ($user in $ADusers)
{

    $username   = $user.username
    $password   = $User.password
    $firstname  = $user.firstname
    $Lastname   = $user.lastname
    $OU         = $user.ou

    {
        New-ADUser
        -SamaccountName $username '
        -Name "$firstname $lastname" 
        -ChangePasswordatlogon $True '
        -Givenname $firstname '
        -Surename $lastname '
        -path $OU 
   }
}

1 Answer 1

1

In your code New-ADUser is running without any parameters.

When splitting a command over multiple lines, you need to use a backtick at the end of each line to signify the command continues on the next line.

If you don't do this, the preceding lines aren't linked to the line above.

Import-Module ActiveDirectory
$ADusers = Import-csv C:\TEST\Create.CSV

Foreach ($user in $ADusers)
{
    $username   = $user.username
    $password   = $User.password
    $firstname  = $user.firstname
    $Lastname   = $user.lastname
    $OU         = $user.ou

    New-ADUser `
        -SamaccountName $username `
        -Name "$firstname $lastname" `
        -ChangePasswordatlogon $True `
        -Givenname $firstname `
        -Surename $lastname `
        -path $OU
}

Note: The indenting isn't required in PS, but makes it easier to read and understand the lines are linked to the Command above.

Sign up to request clarification or add additional context in comments.

3 Comments

It's still not working... sorry i'm pretty new with powershell Foreach ($user in $ADusers) { $username = $user.username $password = $User.password $firstname = $user.firstname $Lastname = $user.lastname $OU = $user.ou } { New-ADUser -SamaccountName $username -Name "$firstname $lastname" -ChangePasswordatlogon $True -Givenname $firstname -Surename $lastname -path $OU }
@Remco, looks like you have placed brackets "{" to the wrong place. So you now have New-ADUser running out of the foreach loop.
Updated answer to make it clearer - you don't need the second set of { }, the backticks are instead of these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.