0

I type this Powershell command

PS C:\Users\User>  new-aduser -name "Tracy Jones" -givenname "Tracy" -surname "Jones" -samaccountname "tjones" userprincipalname "tjones@adatum.com" -path "ou=marketing,dc=abc,dc=com" -accountpassword(-Read-Host -assecurestring "type password for user") -enables $true

It shows me this kind of error message:

-Read-Host : The term '-Read-Host' 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 line:1 char:184

  • ... path "ou=marketing,dc=abc,dc=com" -accountpassword(-Read-Host -assecu ...

  •                                                    ~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (-Read-Host:String) [], CommandNotFoundException

    • FullyQualifiedErrorId : CommandNotFoundException

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 2
    You probably want to add a space after account password and remove the dash infront of Read-Host. But I'm not sure, if calling Read-Host this way is even supported. Maybe you'll have to read the password first and call new-aduser in a second step – derpirscher Sep 27 '20 at 16:05

2 Answers2

0

You can try this:

$pass=Read-Host -Prompt "Type password for user" -AsSecureString -Force
$user=@{
Name="Tracy Jones" 
givenname="Tracy" 
surname="Jones"
samaccountname="tjones" 
userprincipalname="tjones@adatum.com"
path="ou=marketing,dc=abc,dc=com"
accountpassword=$pass
enabled=$true
}
new-aduser @user
  • Read-Host in separate variable and statement.
  • Enables corrected to Enabled
  • Done splatting for long cmdlet
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Tracy needs an opening quote character for givenname ;) – Theo Sep 27 '20 at 19:15
  • Oh @Theo sorry for the mistake while copying, didn't notice it, edits are welcome in this case – Wasif Sep 28 '20 at 07:15
  • These things happen, especially now I think that SO has changed the Syntax Highlighter and because of that almost all PowerShell code suddenly looks like it's almost all olive-greenish comments. Small mistakes now don't really stand out anymore.. – Theo Sep 28 '20 at 08:14
0

$passvalue = Read-Host -assecurestring "type password for user". Take the password in a variable then use it as the value for -accountpassword parameter.