New-ADUser : The object name has bad syntax At…
- 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
- Assistant
These error messages indicate two key issues in your PowerShell script related to adding new users to Active Directory using
New-ADUser.-
The object name has bad syntax: This error happens because there is something wrong with the syntax of the CN (Common Name) or another attribute you're trying to set for the new AD user. It's flagged at the point where the command
New-ADUseris being executed. Often, this is because of incorrect use of escape characters, spaces, or unsupported characters in the distinguished name or other fields. Double-check the syntax of your CN and other parameters to ensure they are correct. If you're specifying the DN (Distinguished Name) or CN, ensure it's in the correct format and doesn't include illegal characters or formatting. -
-AccountPassword is not recognized: This error suggests a syntactical problem around the
AccountPasswordparameter. It appears that PowerShell is not recognizing-AccountPasswordas a part of the command it is executing. This problem could stem from an issue such as incorrect line continuation or a misplaced parameter.
For the first issue, ensure your
New-ADUsercommand's syntax is correct. For example:New-ADUser -Name "JohnDoe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "jdoe@example.com" -Path "OU=Users,DC=example,DC=com"Replace the placeholder values with the actual ones you intend to use.
For the second issue, ensure you are correctly continuing the command on the next line. In PowerShell, if you're breaking a command across multiple lines for readability, you should use the backtick (`) character as the line continuation character or ensure your parameters are correctly lined up in a way that PowerShell can interpret. For example:
New-ADUser -Name "JohnDoe" ` -AccountPassword (ConvertTo-SecureString "Passw0rd2023" -AsPlainText -Force) ` -OtherParameters ...Make sure that the
-AccountPasswordparameter directly follows other parameters without any unintended breaks not marked by line continuation characters.Please check your script around lines 30 and 44 where these issues are mentioned. Correcting the syntax and ensuring proper use of parameters should resolve the errors.
-
- User
#This script is used to create new user accounts in Active Directory based on input from a CSV file. #Created by - md, modified by (name) on April 6, 2024. #The script must be run as a member of the Domain Admins group and with an Unrestricted Execution Policy for this process only.
#Selects the input CSV file containing the user data dataSource = Import-CSV "NETWusersV3.csv" #Uses a foreach loop to create multiple users from the input CSV file foreach (dataRecord in dataSource) { #Constructs the common name (CN) for the user account cn = dataRecord.Butler #creates the sAMAccountName for the user account dataRecord.FirstInitial + sAMAccountName = $sAMAccountName.ToLower()
#Extracts the first and last name from the input data dataRecord.FirstInitial dataRecord.LastName
#Creates the display name for the user account sn + ", " + $givenName
#Extracts the department and organizational unit (OU) from the input data dataRecord.Dept dataRecord.OU
Constructs the user principal name (UPN) for the user account
sAMAccountName + "@ljbs.netw1500.ca"
#Creates the new user account with the specified parameters New-ADUser
-SamAccountName $sAMAccountName-UserPrincipalName cn-GivenName $givenName-Surname displayName-Path $ou-Department dept-Enabled $true-AccountPassword (ConvertTo-SecureString "Passw0rd2023" -AsPlainText -Force)-AccountPassword (convertto-securestring Passw0rd2023 -AsPlainText -Force)}
- Assistant
The issue with your script primarily lies in the repeated use of the
-AccountPasswordparameter within theNew-ADUsercommand. Duplicate parameters can cause syntax errors and are likely the reason PowerShell is throwing the error messages you've encountered. To fix this, you need to remove the redundant-AccountPasswordparameter and make sure each parameter is properly defined and used only once.Let's correct your script by removing the repeated use of the
-AccountPasswordparameter. Below is an improved version of your script with the correction applied:# This script is used to create new user accounts in Active Directory based on input from a CSV file. # Created by - md, modified by (name) on April 6, 2024. # The script must be run as a member of the Domain Admins group and with an Unrestricted Execution Policy for this process only. # Selects the input CSV file containing the user data $dataSource = Import-CSV "NETWusersV3.csv" # Uses a foreach loop to create multiple users from the input CSV file foreach ($dataRecord in $dataSource) { # Constructs the common name (CN) for the user account $cn = $dataRecord.Liam + " " + $dataRecord.Butler # Creates the sAMAccountName for the user account $sAMAccountName = $dataRecord.FirstInitial + $dataRecord.LastName $sAMAccountName = $sAMAccountName.ToLower() # Extracts the first and last name from the input data $givenName = $dataRecord.FirstInitial $sn = $dataRecord.LastName # Creates the display name for the user account $displayName = $sn + ", " + $givenName # Extracts the department and organizational unit (OU) from the input data $dept = $dataRecord.Dept $ou = $dataRecord.OU # Constructs the user principal name (UPN) for the user account $userPrincipalName = $sAMAccountName + "@ljbs.netw1500.ca" # Creates the new user account with the specified parameters New-ADUser ` -SamAccountName $sAMAccountName ` -UserPrincipalName $userPrincipalName ` -Name $cn ` -GivenName $givenName ` -Surname $sn ` -DisplayName $displayName ` -Path $ou ` -Department $dept ` -Description $dept ` -Enabled $true ` -AccountPassword (ConvertTo-SecureString "Passw0rd2023" -AsPlainText -Force) }Before executing the script, ensure that the CSV file "NETWusersV3.csv" is structured correctly with the expected columns (
Liam,Butler,FirstInitial,LastName,Dept,OU). Also, confirm that the Organizational Unit (OU) paths specified in the CSV are accurate and exist in your Active Directory environment. The-Pathparameter inNew-ADUserrequires a distinguished name (DN) of the OU where the user will be created, e.g.,"OU=Sales,DC=example,DC=com".Lastly, ensure your execution policy permits running scripts and that you have necessary permissions in Active Directory to create user accounts.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.