1

I am trying to upload bulk users to my AD using the code below. But it keep giving me an error.

import-csv C:\Users\Administrator\Desktop\properties1.csv |ForEach-Object {New-ADUser -path "OU=Users,OU=ICTLAB,DC=gdgshj,DC=com" -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Name $_.Name -DisplayName $_.DisplayName -profilepath $_.Profilepath Set-ADAccountPassword -Identity $_.SamAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $_.Password -Force) Enable-ADAccount -Identity $_.SamAccountName }

The error is as attached.

Error in powershell

1 Answer 1

3

You need a statement terminator for the New-ADUser cmdlet, before calling Set-ADAccountPassword.

You can either use ;, or place Set-ADAccountPassword on a separate line:

Import-Csv C:\Users\Administrator\Desktop\properties1.csv |ForEach-Object {
    New-ADUser -path "OU=Users,OU=ICTLAB,DC=gdgshj,DC=com" -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Name $_.Name -DisplayName $_.DisplayName -profilepath $_.Profilepath 
    Set-ADAccountPassword -Identity $_.SamAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $_.Password -Force) 
    Enable-ADAccount -Identity $_.SamAccountName 
}

You can also chain the commands together in a single pipeline, if you use the -PassThru switch on New-ADUser and Set-ADAccountPassword (notice the | at the end of each line):

Import-Csv C:\Users\Administrator\Desktop\properties1.csv |ForEach-Object {
    New-ADUser -path "OU=Users,OU=ICTLAB,DC=gdgshj,DC=com" -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Name $_.Name -DisplayName $_.DisplayName -profilepath $_.Profilepath -PassThru |
    Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $_.Password -Force) -PassThru | 
    Enable-ADAccount
}
Sign up to request clarification or add additional context in comments.

1 Comment

I also need to add the domain name after the USER LOGON NAME '@gdgshj.com' I would also like to Enable, PASSWORD NEVER EXPIRES and USER CANNOT CHANGE PASSWORD options under the account tab. How do I add those codes to this powershell script.

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.