Hey Everyone,

I am currently trying to create a large group of users in active directory using powershell. I am somewhat new to powershell, and I am coming across some issues.

Here is the code I am trying to run:

$Users = Import-Csv -Path "<path>"           
foreach ($User in $Users)           
{           
    $Displayname = $User.'Firstname' + " " + $User.'Lastname'           
    $UserFirstname = $User.'Firstname'           
    $UserLastname = $User.'Lastname'           
    $OU = $User.'OU'           
    $SAM = $User.'SAM'                     
    $Description = $User.'Description'           
    $Password = $User.'Password'           
    New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -server domain.loc           
}

When I run the script above, I am getting the error “New-ADUser : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.”

I checked the AWDS and it is running, and I am running this script directly from the domain controller, so I am not sure why it is unable to contact the DC.

Any help is appreciated!

2 Spice ups

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

If you have just one domain, you don’t really have to give it the server.

Also $UPN is not defined.

Maybe look into splatting

Edit: it’s called splatting, not ‘platting’ lol >.>

1 Spice up
foreach ($User in (Import-Csv -Path "<path>"){
    $hash = @{
        Name                  = $User.'Firstname' + " " + $User.'Lastname'
        DisplayName           = $User.'Firstname' + " " + $User.'Lastname'
        SamAccountName        = $User.'SAM'
        UserPrincipalName     = $upn # does not seem to be defined?
        GivenName             = $User.'Firstname'
        Surname               = $User.'Lastname'
        Description           = $User.'Description'
        AccountPassword       = (ConvertTo-SecureString $($User.'Password') -AsPlainText -Force)
        Enabled               = $true
        Path                  = $User.'OU'
        ChangePasswordAtLogon = $false
        PasswordNeverExpires  = $true  # bad practise...tut tut
        server                = 'dc01' # This would be the actual name of the server,
        # maybe try to just remove the server
    }
    New-ADUser @hash
}
4 Spice ups
New-ADUser -Name $FullName -AccountPassword (ConvertTo-SecureString $password -AsPlainText -force) -GivenName $User.FirstName  -Path $OU -SamAccountName $SAM -Surname $User.LastName  -UserPrincipalName $UPN Enabled $TRUE

why you are running this remotely?

2 Spice ups

Is this a Server 2003 domain?

2 Spice ups

Thanks for the help! It looks like it was the server specification causing the issue. I removed that and defined the upn and it went through no problem.

That’s what I get for copy and paste from technet. Thanks!

1 Spice up

try this if you have any issues later

https://community.spiceworks.com/scripts/show/3682-bulk-create-active-directory-users-powershell-with-logs-less-rows-in-csv

1 Spice up