Category filter
Script to delete local user accounts on Windows
You might have configured multiple user accounts on your Windows devices in a shared-device environment. However, over time, there can be one or more user accounts that are not being used. In such scenarios, you can delete those user accounts as they are no longer necessary. Using Hexnode, you can remotely run custom scripts on your Windows devices to delete any specific user account or all user accounts except the specified ones.
Batch script to delete a specific user account
1 |
net user %1 /delete |
Specify the username of the account to be deleted in the Arguments space when executing the above script from the Hexnode portal.
PowerShell script to delete a specific user account
1 2 |
param([string] $user_name="") net user $user_name /delete |
Specify the username of the account to be deleted in the Arguments space when executing the script via the Hexnode portal.
PowerShell script to delete all user accounts except specific user accounts
1 2 |
$UserList = @("Administrator","DefaultAccount","Guest","IEUser","sshd","WDAGUtilityAccount") Get-WMIObject -Class Win32_UserAccount | Where-Object {$_.Name -notin $UserList} | Foreach {net user $_.Name /delete} |
You can use the above script to delete all the user accounts on the device other than the required ones. Specify the username of the accounts to be retained on the device corresponding to the $UserList variable. You can specify multiple user accounts separated by commas.
For example, the below script will delete all the user accounts except DefaultAccount, Guest, IEUser and sshd:
1 2 |
$UserList = @("DefaultAccount","Guest","IEUser","sshd") Get-WMIObject -Class Win32_UserAccount | Where-Object {$_.Name -notin $UserList} | Foreach {net user $_.Name /delete} |
You can run the Get-LocalUser command to get a list of all the user accounts on the device before deciding on the user accounts to be deleted.