Youssef Karami
Youssef Karami

Reputation: 75

Add users to Active Directory

I'm trying to use the script below to add new users to Active Direcroty but for some reason I keep gettings error messages:

The Script:

Import-Module ActiveDirectory
Import-Csv 'C:\Scripts\\AddUsers.csv' -Delimiter "," | ForEach-Object {
 $userPrincinpal = $_."SAM" + "@domain.org"
New-ADUser 
 -Name $_.Name `
 -GivenName $_."First_Name" `
 -Surname $_."Last_Nimpoame" `
 -Description "Student"
 -Path $_."OU" `
 -SamAccountName  $_."SAM" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "password2016" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
}

Write-Host "Done!"

The error message:

-Name : The term '-Name' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At C:\Scripts\Add Bulk AD User CSV\add_ad_users2.ps1:5 char:2
+  -Name $_.Name `
+  ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Name:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At C:\Scripts\Add Bulk AD User CSV\add_ad_users2.ps1:9 char:2
+  -Path $_."OU" `
+  ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Path:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I'm not sure why I keep getting those error messages since I already imported the ActiveDirectory Module.

Can you please help?!

Upvotes: 1

Views: 920

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59001

You are missing a trailing backtick after the New-ADUser:

Import-Module ActiveDirectory
Import-Csv 'C:\Scripts\\AddUsers.csv' -Delimiter "," | ForEach-Object {
 $userPrincinpal = $_."SAM" + "@domain.org"
New-ADUser `
 -Name $_.Name `
 -GivenName $_."First_Name" `
 -Surname $_."Last_Nimpoame" `
 -Description "Student"
 -Path $_."OU" `
 -SamAccountName  $_."SAM" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "password2016" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
}

Write-Host "Done!"

Upvotes: 2

Related Questions