PowerShell: create a new Active Directory user with password as a secure string

Having some trouble creating a command to create a new user in PowerShell, the code I'm using is:

PS C:\Users\Nicky> New-ADUser -Name johnd -GivenName John -Surname Doe -DisplayName John Doe $secpasswd = ConvertTo-SecureString -String "pa$$word1" -AsPlainText -Force -AccountPassword $secpasswd

I can get it working without the AccountPassword, so I think the issue is when converting it to a secure string so the -AccountPassword will accept it.

Anyone any ideas on how to get this to work?

1 Answer

Your syntax is wrong. Take a look at New-ADuser help page from Microsoft and this one from SS64. Also if you have spaces in your string (example: John Doe) you should single quote it.

It should have a syntax similar to this:

New-ADUser [-Name] <string> [-AccountExpirationDate <System.Nullable[System.DateTime]>]...

You might want to do this instead:

$secpasswd = ConvertTo-SecureString -String "pa$$word1" -AsPlainText -Force
New-ADuser -Name 'johnd' -GivenName'John' -Surname 'Doe' -DisplayName 'John Doe' -AccountPassword $secpasswd
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like