I am working on importing users into AD in bulk and have a script:
Import-Module ActiveDirectory
$path = Split-Path -Parent $MyInvocation.MyCommand.Definition
$newpath = $path + "\import_users.csv"
# Define variables
$log = $path + "\created_ActiveDirectory_users.log"
$date = Get-Date
$i = 0
function createActiveDirectoryUsers {
"Created the following Active Directory users (on " + $date + "): " | Out-File $log -Append
"--------------------------------------------" | Out-File $log -Append
Import-Csv $newpath | ForEach-Object {
$samAccount = $_.SamAccountName
try {
$exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccount)"
} catch { }
if (!$exists) {
$i++
# Set all variables according to the table names in the Excel
# sheet / import CSV. The names can differ in every project, but
# if the names change, make sure to change it below as well.
$setpass = ConvertTo-SecureString -AsPlainText $_.Password -Force
New-ADUser -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials `
-Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
-Description $_.Description -EmailAddress $_.eMail `
-StreetAddress $_.StreetAddress -City $_.L `
-PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
-Company $_.Company -Department $_.Department -EmployeeID $_.ID `
-OfficePhone $_.Phone -AccountPassword $setpass -Enabled $true -Path $_.OU
$output = $i.ToString() + ") Name: " + $_.CN + " sAMAccountName: "
$output += $sam + " Pass: " + $_.Password
$output | Out-File $log -append
} else {
"SKIPPED - USER ALREADY EXISTS OR ERROR: " + $_.CN | Out-File $log -append
}
}
"----------------------------------------" + "`n" | Out-File $log -append
}
createActiveDirectoryUsers
However when I try to import the CSV file, I am getting the following error:
New-ADUser : The object name has bad syntax At C:\temp\bulk_create_users.ps1:33 char:17 + New-ADUser <<<< -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials ` + CategoryInfo : NotSpecified: (CN=Ranae Gentry...C=abbhq,DC=com":String) [New-ADUser], ADException + FullyQualifiedErrorId : The object name has bad syntax,Microsoft.ActiveDirectory.Management.Commands.NewADUser
Not exactly sure what syntax is wrong here.
$_.DisplayName
when the error occurs? – Ansgar WiechersNew-ADUser
withWrite-Debug
orWrite-Host
in order to easier troubleshoot what might be going on – Mathias R. Jessen