Welcome to MSDN Blogs Sign in | Join | Help

Add a user to a local group

This scripts shows how to add a user to a local group on a machine.

VBScript Example

function add_to_local_group( machine, account, local_group_name ) 
	set object_to_add = GetObject("WinNT://" & account )
	set local_group = GetObject("WinNT://" & machine & "/" + local_group_name & ",group")
	local_group.Add( object_to_add.AdsPath )
end function

this_machine = "."
domain_account = "woodgrove/kevinp" ' Information about the item to add
admin_group_name = "Administrators" ' the group in which to place that item

call add_to_local_group(this_machine, domain_account, admin_group_name )

 

 

Python Example

# ----------------------------------------
# SCRIPT: add_user_to_local_group.py
#
# EXAMPLE: Add "domain\user1" to administrators group on local machine
# add_user_to_local_group.py "" domain\user1 Administrators
# add_user_to_local_group.py localhost domain\user1 Administrators
#
# NOTE: this is case-insensitive
# NOTE: requires win32 extensions for pythoon
#

import os
import win32net
import win32netcon
import sys

def add_user_to_local_group( computer, account, localgroup, ) :
    localgroup = unicode(localgroup).lower()
    groups = win32net.NetUserGetLocalGroups( computer, account, 0 )
    groups = [ s.lower() for s in groups ]

    if (localgroup in groups ) :
        #user is a already in the group
        pass
    else :
        #must add the user
        level =3
        members = []
        members.append( {"domainandname" : account } )
        win32net.NetLocalGroupAddMembers( computer, localgroup, level, members  )

    # postconditions
    groups = win32net.NetUserGetLocalGroups( computer, account, 0 )
    groups = [ s.lower() for s in groups ]
    assert( localgroup in groups )

the_computer = sys.argv[1]
the_account_name = sys.argv[2]
the_group = sys.argv[3]

add_user_to_local_group( the_computer, the_account_name , the_group, )


# ----------------------------------------
    

Saveen Reddy
2005-10-08

Published Saturday, October 08, 2005 2:05 PM by saveenr

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Add a user to a local group

Thanks, just the code snippet I needed :-)

Cheers
Wednesday, November 30, 2005 7:46 AM by senkwe

# re: Add a user to a local group

This was EXTREMELY helpful at work. THANK YOU! I'm referring to the VBS version.

Thursday, November 01, 2007 1:44 PM by DeployPenguins

# re: Add a user to a local group

The VB example was great.

Thank you.

Thursday, July 31, 2008 9:21 AM by Zak

# re: Add a user to a local group

Thanks you much for the VB snippet.  Helped out Big time.

Wednesday, November 19, 2008 3:03 PM by KA

# re: Add a user to a local group

Hello Saveen,

How do I address it in order to remotely add local admin user & pwd into several servers?

Thanks,

Fernando

Friday, May 01, 2009 1:40 PM by Fernando

# re: Add a user to a local group

Your sript doesn't work when I try to add group from the domain which this computer "." is not belog to.

Tuesday, August 18, 2009 1:11 PM by davtup

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker