Script to Delete Inactive Local User Accounts
In this article, we will discuss a script that can be used to delete local user accounts on a PC that haven't been logged into for the past three months. This can be useful for maintaining a clean and organized system, especially in a corporate environment where user accounts may be created and abandoned over time.
Prerequisites
Before we begin, it is important to note that this script should only be run on PCs where you have permission to delete user accounts. Additionally, you should always make sure to back up any important data before running a script like this, as it will permanently delete user accounts and all of the data associated with them.
Approach
The approach we will take to create this script is to use the Windows Management Instrumentation (WMI) to query the system for information about user accounts and their last login time. We will then use this information to identify user accounts that haven't been logged into for the past three months, and delete them using the built-in net user command.
Script
Here is an example script that implements this approach:
import datetime
import subprocess
# Set the number of months ago that we consider to be "inactive"
inactive\_threshold = datetime.timedelta(weeks=12)
# Use the WMI to query the system for information about user accounts
wmi = subprocess.check\_output(["wmic", "useraccount", "get", "Name,LastLogon"]).decode("utf-8").split("
")
# Iterate through the user accounts and identify any that are inactive
for account in wmi[1:]:
account = account.strip().split("\t")
if len(account) > 1:
last\_logon = datetime.datetime.strptime(account[1], "%Y%m%d%H%M%S.%f")
if (datetime.datetime.now() - last\_logon) > inactive\_threshold:
subprocess.check\_call(["net", "user", account[0], "/delete"])
Explanation
The script starts by setting the number of months ago that we consider to be "inactive" (in this case, 3 months or 12 weeks). It then uses the wmic command to query the system for information about user accounts and their last login time. This information is returned as a list of strings, which we iterate through to identify any user accounts that are inactive.
For each user account, we first check to make sure that the account has a last login time (some built-in accounts, like the "Administrator" account, may not have a last login time). We then convert the last login time from a string to a datetime object, and calculate the difference between the current time and the last login time. If the difference is greater than the inactive threshold, we delete the user account using the net user command.
In this article, we have discussed a script that can be used to delete local user accounts on a PC that haven't been logged into for the past three months. This can be a useful tool for maintaining a clean and organized system, but it is important to always make sure to back up any important data before running a script like this, and to only run it on PCs where you have permission to delete user accounts.
References
- Windows Management Instrumentation (WMI) https://docs.microsoft.com/en-us/windows/win32/wmisdk/wmi-start-page
- Net User Command https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/net-user