6

I've installed PowerShell recently and one of the first things I started looking for was how to create a new user. After looking for some time I still haven't found this. I have a little experience in bash on linux and find it very effective. Creating users there is trivial. Is there an easy\built-in way to create a local user with PowerShell?

Thank you.

2 Answers 2

20

You can use the localhost's ADSI:

function create-account ([string]$accountName = "testuser") {   
   $hostname = hostname   
   $comp = [adsi] "WinNT://$hostname"  
   $user = $comp.Create("User", $accountName)   
   $user.SetPassword("Password1")   
   $user.SetInfo()   
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you're scripting creating a user with a password, you probably also want that user's password to not expire: $user.Put('userFlags', [int]$user.userFlags.Value -bor 0x00010000) # ADS_UF_DONT_EXPIRE_PASSWD
7

you can also use

net user /add

this command isn't limited to powershell.

Comments

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.