Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

New-ADUser : The object name has bad syntax At…

  1. User

    New-ADUser : The object name has bad syntax At C:\Users\lbTechAdmin\desktop\NETWaddUsers_v25.2.22.ps1:30 char:1

    • New-ADUser `
    • CategoryInfo : NotSpecified: (CN=\ ,Exec_gp:String) [New-ADUser], ADException
    • FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    -AccountPassword : The term ‘-AccountPassword’ 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:\Users\lbTechAdmin\desktop\NETWaddUsers_v25.2.22.ps1:44 char:13

    • -AccountPassword (convertto-securestring Passw0rd2023 -As …
    • CategoryInfo : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException
  2. Assistant

    From the error messages you've provided, it looks like you're encountering two different issues while running your PowerShell script to create new Active Directory (AD) users. Let's break down these errors and discuss potential resolutions for each.

    1. The object name has bad syntax

    New-ADUser : The object name has bad syntax
    ...
    + CategoryInfo : NotSpecified: (CN=\ ,Exec_gp:String) [New-ADUser], ADException
    

    This error typically means that there's a problem with how an object's name is being specified in your New-ADUser command. The mention of CN=\ ,Exec_gp suggests there might be a problematic space or comma, or perhaps escape characters are being used incorrectly. Here are common reasons and fixes:

    • Incorrect DN syntax: Ensure the Distinguished Name (DN) is correctly formatted. If you're specifying a DN, it normally looks like "CN=John Doe,OU=Users,DC=example,DC=com". Make sure there are no unintended spaces or backslashes (\).
    • Unnecessary escaping: If you're using backquotes () to continue the command on the next line, ensure they're correctly placed and necessary. PowerShell uses the backtick as the escape character, not the backslash (`), unless you're inside a string.
    • Incorrect variable usage: If variables are part of your command, ensure they're correctly specified and contain the expected values.

    2. -AccountPassword cmdlet not recognized

    -AccountPassword : The term ‘-AccountPassword’ is not recognized as the name of a cmdlet, function, script file, or operable program.
    ...
    

    This error happens because PowerShell interprets -AccountPassword as a standalone command rather than a parameter of New-ADUser, likely due to incorrect line continuation. PowerShell has specific requirements for line continuation:

    • Inline approach: Use backticks (`) at the end of each line you want to continue, ensuring there's no space after the backtick.
    • Single command block: Encapsulate your command in brackets (), which allows for clearer multi-line commands without needing backticks.

    Here's an example of using both solutions for New-ADUser:

    # Example with backticks
    New-ADUser `
        -Name "John Doe" `
        -GivenName "John" `
        -Surname "Doe" `
        -AccountPassword (ConvertTo-SecureString "Passw0rd2023" -AsPlainText -Force) `
        -Enabled $true
    
    # Example with brackets for cleaner syntax
    New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -AccountPassword (ConvertTo-SecureString "Passw0rd2023" -AsPlainText -Force) -Enabled $true
    

    Summary

    • Ensure the DN syntax is correct without erroneous spaces, backslashes, or commas.
    • Correctly use the line continuation approach with backticks if breaking commands across multiple lines, or consider using a single command block for clarity.
    • Revisit the script for any syntax inaccuracies or missing parameters that might have been overlooked initially.
    • If including a DN, ensure its format and values are accurate without unintended characters.