title: VBS : Create a local admin account
body: <p>I'm using the VBS script below to create a local user account and add it to the administrators group, however when I run it ,it creates the account but I get an exception when it tries to add it to the local admin group.</p>
  
  <pre><code>    Const ADS_UF_DONT_EXPIRE_PASSWD = &amp;h10000
  
  ' specify account to create 
  strAccount = "user"
  strPswd = "password"
  
  ' get local computer name 
  Set objNetwork = CreateObject("Wscript.Network")
  strComputer = objNetwork.ComputerName
  
  ' check if local account already exists
  intExists = 0
  Set colAccounts = GetObject("WinNT://" &amp; strComputer &amp; "")
  colAccounts.Filter = Array("user")
  For Each objUser In colAccounts
      If objUser.Name = strAccount Then
          intExists = 1
      End If
  Next
  
  If intExists = 0 Then
  
     ' create local user 
     Set colAccounts = GetObject("WinNT://" &amp; strComputer &amp; "") 
     Set objUser = colAccounts.Create("user", strAccount) 
  
     ' set pswd 
     objUser.SetPassword strPswd
     objUser.SetInfo
  
     ' add to local admins group 
     Set objGroup = GetObject("WinNT://" &amp; strComputer &amp; "/Administrators,group")
     Set objUser = GetObject("WinNT://" &amp; strComputer &amp; "/" &amp; strAccount &amp; ",user")
     objGroup.Add(objUser.ADsPath)
  
     ' set password to not expire
      intUserFlags = objUser.Get("UserFlags")
      objUser.put "Userflags", intUserFlags Or ADS_UF_DONT_EXPIRE_PASSWD
      objUser.SetInfo
  
  End If
  </code></pre>
  
  <p>here is the error message i'm getting :</p>
  
  <p><a href="https://i.stack.imgur.com/CBuqX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CBuqX.jpg" alt="CBuqX.jpg"></a></p>