-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows-admin-remover.py
42 lines (29 loc) · 1.42 KB
/
windows-admin-remover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess
import re
# Listing all users of the Windows system
def list_users():
list_users_command = "net users"
result = subprocess.run(list_users_command, shell=True, capture_output=True, text=True).stdout
# Extracting only usernames using regex patterns
# Slicing is done to remove unnecessary ouput generated by 'net users' command
usernames = re.findall(r"\b[A-Za-z]+\b", result)[4:-4]
print("The users in this Windows system are: \n")
for user in usernames:
print(user)
print("\n")
# Adding user to 'Users' group
def add_user_to_group(username):
add_user_to_group = f"net localgroup Users {username} /add"
subprocess.run(add_user_to_group, shell=True, check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
print(f"The user '{username}' added to 'Users' group successfully \n")
# Removing user from 'Administrators' group
def remove_user_from_group(username):
remove_user_from_group = f"net localgroup Administrators {username} /delete"
subprocess.run(remove_user_from_group, shell=True, check=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
print(f"The user '{username}' removed from 'Administrators' group successfully \n")
# Getting list of users
usernames = list_users()
# Asking user to enter the username from the list
username = input("Enter the username from the above list: ")
add_user_to_group(username)
remove_user_from_group(username)