We help IT Professionals succeed at work.

Create Local User Account specifying domain with VBS

cbielich asked
Medium Priority
Last Modified: 2012-05-11
I have a vbs script that works in creating a local user account as an administrator on a target computer. The only thing is that it creates it as a local admin if that computer as the domain. How can I specify a different domain, or should I say the domain of AD which would be "GVCC"
' get target machine name and password 
' from command line arguments 
strServer = wscript.arguments(0)
strUser = wscript.arguments(1)
strPwd = wscript.arguments(2) 
' connect to target machine and  
' create new user 
Set oServer = GetObject ("WinNT://" & strServer) 
Set oUser = oServer.Create ("user", strUser)

oUser.SetPassword strPwd 

' add new user to 'Administrators' group 
Set Group = GetObject("WinNT://" & strServer & "/administrators,group") 
' release objects 
Set oServer = nothing 
Set oUser = nothing 
Set group= nothing

Open in new window

Watch Question

You are using the older WinNT ADSI provider, and as such it only references local machines. If you want to add an administrative account to your domain, you might want to explore using ADO to query your Active Directory server.


I dont want to add an administrative account to my domain I want to add an domain users as an administrator of a local machine. Can ADO do this? Is there no option for this with the code I have now?
Hi cbielich,

My apologies for misunderstanding. What you'll need to do is create one isntance of your WinNT ADSI provider that can talk to your domain, then another that talks to your local machine's Administrators group. Then, get the user you want from the domain instance, and add it to the Administrator group on the local box.

You would first set your oUser object to your desired domain account:

oUser  = GetObject('WinNT://GVCC/BobSmith, User")

Open in new window

You've already bound to the local group you want, so then just add the user to that group - and you've already done that! So you're 90% of the way there!

i hope this has helped!



Can you show me how I need to integrate that into my existing code please :)
In the line where you set oUser, replace it with the line I provided and replace the "BobSmith" name with the name of the user you want to pull from the domain....

The difference here is that you're not creating a user, you're simply binding to an existing user that you can add to the local machine's administrator's group.


So do I use "Set" before it?

I replaced the line with

Set oUser  = GetObject('WinNT://GVCC/strUser, User")

and I get this error

Microsoft VBScript compilation error: Syntax error
Unlock this solution and get a sample of our free trial.
(No credit card required)
One other itme - You definitely need the "Set" keyword in front of the assignment. Any time you set a variable to an object reference in VBScript you need to use the Set keyword. You don't have to do it when you're setting regular values like strings or numbers.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.