How to create Windows user with Powershell?

You can create Windows user from Powershell:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function create_user($username, $password)
	{
		$computer = [ADSI]"WinNT://localhost"
		foreach ($user in $computer.psbase.children)
			{
				if ($user.Name -eq $username)
					{
						Write-Host $user.Name "already exist."
						Return
					}
			}
		$user_obj = $computer.Create("user", "$username")
		$user_obj.Put("description", "$username")
		$user_obj.SetInfo()
		$user_obj.SetPassword($password)
		$user_obj.SetInfo()
		$user_obj.psbase.invokeset("AccountDisabled", "False")
		$user_obj.SetInfo()
		Write-Host "$username created."
	}
 
create_user "$username" "$password"

It checks user and if it is not exist, creates that username.

Tags: , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

4 Responses to “How to create Windows user with Powershell?”

  1. what about to create user on remote host (server)?
    i’ve tried to replace the
    $computer = [ADSI]“WinNT://localhost”
    to
    $computer = [ADSI]“WinNT://192.168.1.10″ but i got this error :
    Exception calling “SetInfo” with “0″ argument(s): “Logon failure: unknown username or bad password. (Exception from HRESULT: 0x8007052E)”
    At line:14 char:26
    + $user_obj.SetInfo <<<< ()
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    please help me!

    thanks.

  2. Hello Herry,

    I wrote a script to create user remote host. I’ll try to post you today, it’s on office pc.

    But you can try Remote Powershell for all scripts. Just google for New-PSSession and Enter-PSSession.

    See you.

  3. Hello Herry,

    You need to have admin privileges on all remote servers. You can set same username and password on all servers or you can join them to an active directory. Using PSexec or Remote Powershell are other options.

  4. you might want to add an additional if statement at the end to actually verify if the user got created or not.

    I ran this today on localhost and got an error and still got the output user created which obviously wasnt right.

    function create-user($username, $password)
    {

    $computer = [ADSI]“WinNT://localhost”
    foreach ($user in $computer.psbase.children)
    {
    if ($user.Name -eq $username)
    {
    Write-Host $user.Name “already exist.”
    Return
    }
    }
    $user_obj = $computer.Create(“user”, “$username”)
    $user_obj.Put(“description”, “$username”)
    $user_obj.SetInfo()
    $user_obj.SetPassword($password)
    $user_obj.SetInfo()
    $user_obj.psbase.invokeset(“AccountDisabled”, “False”)
    $user_obj.SetInfo()
    if ($user.Name -eq $username)
    {
    Write-Host “$username created.”
    Return
    }
    else
    {
    Write-host “Error Creating $username”
    }
    }

Leave a Reply