Create Local User with VBScript

This script allows to create a local user using VBScript.

 

In this example:

  • Creation of a local user (login “dam” / Password “dampwd”)
  • Selection of the option “User cannot change password”
  • Selection of the option “Password never expire”
  • Selection of the option “Account deactivated”

To play with local users, we use the property ComputerName of the object “WScript.Network

' Declaration
strUser = "dam"
strPassword = "dampwd"

On error Resume Next

'Computer Name
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName

' Creation of the local user
Set colAccounts = GetObject("WinNT://" & strComputer & "" )
Set objUser = colAccounts.Create("user", strUser)
objUser.SetPassword strPassword
objUser.SetInfo

' Select the option "User cannot change password"
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.Run "cmd.exe /C Net User " & strUser & " /passwordchg:no /y"

' Select the option "Password never expire"
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
objUserFlags = objUser.Get("UserFlags")
objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD
objUser.Put "userFlags", objPasswordExpirationFlag 
objUser.SetInfo

' Select the option "Account deactivated"
objUser.AccountDisabled = True
objUser.SetInfo