Series Note: This is Part 2 of a two-part series on Windows command-line architecture. Read Part 1: CMD vs PowerShell: Navigating the Limits
The Python factor
CMD and PowerShell are native shells built directly into Windows. But for developers coming from a web background, Python is often the go-to tool. If you’ve already used Python’s os library to interact with the operating system, you might be tempted to use it to automate the previous challenge: adding a batch of users and passwords to Windows. But is adding an external dependency worth it for OS-level tasks?
Pre-requisites
To execute this, we rely entirely on Python’s standard library. We just import two core modules: os and csv.
import os
import csv
We assume a local file named users.csv contains the target accounts in a standard comma-separated format:
username01,password1
username02,password2
username03,password3
... ...
The Implementation
The complete automation script is detailed beloebelow.
import os
import csv
users_file_path = "users.csv"
print(os.getcwd())
if os.path.exists(users_file_path):
# Open and parse the CSV file
with open(users_file_path, mode='r', newline='', encoding='utf-8') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
# Extract username and password to a variable
username = row[0]
password = row[1]
print(f"Creating: {username}...")
# Execute the native CMD command
exit_code = os.system(f"net user {username} {password} /add")
# Check final status
if exit_code == 0:
print(f"Successfully created user: {username}\n")
else:
print(f"Failed to create user: {username} (Exit Code: {exit_code})\n")
else:
print(f"Error: File not found. Check path of '{users_file_path}'.")
Behind the Scenes
The initial if…else block verifies that the target CSV file exists in Python’s current working directory.
The csv_reader automatically parses the file using a comma as the default delimiter. The parsed strings from each column are then cleanly assigned to the username and password variables.
Then, the os.system() function passes our string command directly to a background command-line process. Python acts as the orchestrator here, typing out net user commands automatically so you don’t have to do it manually.
The script reads the exit code and logs whether the command succeeded or failed.
The real-world “Gotchas”
When running this script in a development environment like IDLE, you will likely hit an immediate roadblock where the script returns an error code.
This isn’t an issue with your CSV path or your Python syntax; it is a Windows security boundary. Modifying system accounts requires elevated administrative rights. If Python IDLE is opened normally, the hidden CMD window it spawns will run with standard user restrictions, and Windows will block the command. To fix this, right-click Python IDLE and select “Run as Administrator”. Because of process inheritance, the administrative security token will pass down from IDLE to Python, and finally to the background CMD process, allowing the accounts to be created successfully.
Conclusion
If you are already fluent in Python, using it for system administration provides massive scalability when parsing data structures like CSVs. The os library offers a quick, direct bridge to the legacy CMD environment. However, for simple configurations, running native commands directly in an elevated CMD window or PowerShell terminal remains the leanest approach without the overhead of managing script files.