Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created April 29, 2016 20:50
Show Gist options
  • Save vlad-bezden/8d21c3d454813147c2e83357b98164bd to your computer and use it in GitHub Desktop.
Save vlad-bezden/8d21c3d454813147c2e83357b98164bd to your computer and use it in GitHub Desktop.
Example on how to add user to the group in Windows using Python
#
# Example on how to add user to the group in Python
#
import win32net
def add_user_to_group(domain, group, user):
user_group_info = {
'domainandname': '%s\\%s' % (domain, user)
}
try:
win32net.NetLocalGroupAddMembers(None, group, 3, [user_group_info])
except win32net.error as error:
number = error.args[0]
if number != 1378:
print("Error Number: '%s'; Context: '%s'; Message: '%s'" % error.args)
raise
# function usage
print(add_user_to_group('SOME_DOMAIN', 'SOME_GROUP', 'SOME_USER'))
@oraant
Copy link

oraant commented Oct 26, 2022

thanks man, that really helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment