none
Not able use comma in name attribute while creating new user in AD using powershell RRS feed

  • Question

  • Hi All,

    Greetings..!!

    i have query with user creation in AD using powershell scripting.

    Am not able to assign ","(comma) to name attribute in New-ADUser

    Ex:

    $Firstname = "John"

    $Secondnam = "Cena"

    $User = "$Secondname, $Firstname"

    New-ADUser -Name $User

    Am getting error message for using above $User variable, probably due to comma after the second name.

    Requesting anyone to kindly consider and suggest on how to achieve the above requirement

    Thanks in advance...!!! 

    Wednesday, July 26, 2017 4:56 PM

Answers

All replies

  • Try escaping the comma with the backslash escape character:

    $User = "$Secondname\, $Firstname"

    Edit:

    Documentation here:

    https://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx

    Edit: Let use know if my suggestion doesn't work. I was under the impression that the New-AD* cmdlets would automatically escape any characters needed when you create objects in AD. It may also be that your Name is not unique in the parent OU/container. Or the Name could exceed 64 characters. The exact error message will help troubleshoot further.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Wednesday, July 26, 2017 5:28 PM
    Moderator
  • Hello Richard,

    Thanks for your suggestion

    Tried but no luck, i have been receiving below error message.

    Could you please assist...?

    PS C:\> $Firstname = "Bob"
    PS C:\>
    PS C:\> $Secondname = "William"
    PS C:\>
    PS C:\> $User = "$Secondname\, $Firstname"
    PS C:\>
    PS C:\> New-ADUser -Name $User
    New-ADUser : The name provided is not a properly formed account name
    At line:1 char:11
    + New-ADUser <<<<  -Name $User
        + CategoryInfo          : NotSpecified: (CN=William\\\, ...C=salian,DC=com:String) [New-ADUser], ADException
        + FullyQualifiedErrorId : The name provided is not a properly formed account name,Microsoft.ActiveDirectory.Manage
       ment.Commands.NewADUser


    Thursday, July 27, 2017 5:06 AM
    • Marked as answer by Yatish Salian Thursday, July 27, 2017 6:56 AM
    • Unmarked as answer by Yatish Salian Thursday, July 27, 2017 6:57 AM
    • Marked as answer by Yatish Salian Thursday, July 27, 2017 6:57 AM
    Thursday, July 27, 2017 5:24 AM
  • Perfect..!!

    Specifying -sAMAccountName did the magic

    Thanks all for the wonderful support.

    Thursday, July 27, 2017 6:59 AM
  • Thank Richard.  He was the one who figured it out.  I guess he forgot about that old post.

    \_(ツ)_/

    Thursday, July 27, 2017 9:32 AM
  • No kidding. I didn't remember that thread from 4 years ago. And the help for New-ADUser is still poorly written. But it makes sense when I think about. If you only supply the Name, the cmdlet will by default assign the same value for sAMAccountName (which is required). But commas are not allowed in sAMAccountNames.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, July 27, 2017 12:02 PM
    Moderator