1

I am getting below Error. while running command by copying from Powershell file, this file i have opened in notepad, than I am running this command by copying and than pasting in Powershell prompt. enter image description here

Please let me know, if i am making some error by copying this file in powershell prompt windows. Below is the Powershell file screenshot. enter image description here

Regards, Param

$Users = Import-Csv -Path "C:\Userlist-sn.csv"            
foreach ($User in $Users)            
{            
    $Displayname = $User.Firstname + " " + $User.Lastname            
    $UserFirstname = $User.Firstname            
    $UserLastname = $User.Lastname            
    $OU = "$User.OU"            
    $SAM = $User.SAM            
    $UPN = $User.Firstname + "." + $User.Lastname + "@" + $User.Maildomain            
    $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 $true –PasswordNeverExpires $false -server "corp.abc.com"            
}
1
  • 2
    To answer your question Please let me know, if i am making some error by copying this file in powershell prompt windows no, the problem isn't because you're copying the powershell text from notepad into the Powershell prompt. New-Aduser: The object name has bad syntax typically comes from errors for csv-imports where the data has whitespace or other errors. edit for spelling Sep 1, 2015 at 15:48

1 Answer 1

0

You need some debug output. Can I also suggest that you replace your string building code with "-f" formatting, a'la sprintf() in other languages. It makes things easier to read/debug/modify, e.g.:

$Displayname = $User.Firstname + " " + $User.Lastname 

...becomes:

$DisplayName = ( "{0} {1}" -f $User.Firstname, $User.Lastname );

It looks like your error is with the "name" attribute. I believe this maps onto the "CN" attribute in AD, but I could be wrong. Adding some debug like the following would help:

Write-Host -Object ( "Attempting to create user with name ""{0}"" and display name ""{0}""" -f $DisplayName );

And finally, you can lose the quotes in the actual New-ADUser call. The reason being that you are passing variables, as opposed to asking PowerShell to interpret your code.

If you can post some debug output, that would be very useful...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .