1
$secPassword = Read-Host "Password" -AsSecureString
New-ADUser
    -Name "XYZ ABC"
    -SamAccountName xyz.a
    -UserPrincipalName "[email protected]"
    -AccountPassword $secPassword
    -Path "cn=Users,dc=ntsh,dc=local"
    -Enabled:$true
    -PasswordNeverExpires:$true
    -CannotChangePassword:$true
    -PasswordNotRequired:$false
    -ChangePasswordAtLogon:$false

It's giving CommandNotFoundException Error for all Parameters. What's wrong in my script ?

3
  • 3
    Backtick does work but it's kinda brittle as there must be no whitespace between backtick and the end of the line. Best practice is to use splatting instead. Commented Nov 13, 2022 at 9:53
  • 1
    And the way you have now set this up, there would be an error in -SamAccountName xyz.a. You need to quote the value, so -SamAccountName 'xyz.a'. Anyway, zett42 is correct. You should always use splatting on cmdlets that take a lot of parameters IMO. Commented Nov 13, 2022 at 11:10
  • @zett42 That's cool. I now did it with splatting also. It 's a cleaner way. @Theo Yes I got this also that I was missing " " quotes. And also I need to put a Backtick and that should be after at least a space. For eg. New-ADUser ` Commented Nov 13, 2022 at 15:34

2 Answers 2

4

IMHO, splatting is the cleanest way to code as per Zett42's suggestion:

$NADUArgs = @{
    Name                  = "XYZ ABC"
    SamAccountName        = "xyz.a"
    UserPrincipalNam      = "[email protected]"
    AccountPassword       = $secPassword
    Path                  = "cn=Users,dc=ntsh,dc=local"
    Enabled               = $true
    PasswordNeverExpires  = $true
    CannotChangePassword  = $true
    PasswordNotRequired   = $false
    ChangePasswordAtLogon = $false
}

New-ADUser @NADUArgs
Sign up to request clarification or add additional context in comments.

Comments

1

The simplest way would be to use the CMDLET in a single line, because it treats them as if they were foreign commands, and the second way if you want to leave it curious, would be to add the symbol ` so that the cmdlet detects that it continues in the next line

The first way:

$secPassword = Read-Host "Password" -AsSecureString
New-ADUser -Name "XYZ ABC" -SamAccountName xyz.a -UserPrincipalName "[email protected]" -AccountPassword $secPassword -Path "cn=Users,dc=ntsh,dc=local" -Enabled:$true -PasswordNeverExpires:$true -CannotChangePassword:$true -PasswordNotRequired:$false -ChangePasswordAtLogon:$false

The second way:

$secPassword = Read-Host "Password" -AsSecureString
New-ADUser -Name "XYZ ABC" `
 -SamAccountName xyz.a `
 -UserPrincipalName "[email protected]" `
 -AccountPassword $secPassword `
 -Path "cn=Users,dc=ntsh,dc=local" `
 -Enabled:$true `
 -PasswordNeverExpires:$true `
 -CannotChangePassword:$true `
 -PasswordNotRequired:$false `
 -ChangePasswordAtLogon:$false

1 Comment

Using line-ending ` for line-continuation is effective, but can you explain what you mean by because it treats them as if they were foreign commands?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.