Category filter
Script to disable user accounts on Windows devices
Effective management of user accounts also includes measures for controlling who has access to organizational resources. One way to limiting device access is to disable unauthorized user accounts when found, as they may pose a risk by allowing unknown users on them. When there are multiple user accounts on shared Windows devices, disabling unused accounts can help reduce potential security risks. There should also be provisions if the organization wants to enable a previously disabled employee account on the devices. IT admins can do this with the help of scripts. Using Hexnode’s Execute Custom Script remote action, the admin can execute the script to disable or enable the user accounts on Windows devices.
Script to disable a user account
1 |
Disable-LocalUser -Name "Username" |
The above PowerShell script uses the Disable-LocalUser cmdlet to disable a user account on Windows devices.
Replace “Username” with the actual name of the user account you wish to disable.
Script to disable multiple user accounts
If you want to disable multiple user accounts, you can use the script below.
1 2 3 4 5 |
$usersToDisable = @("User1", "User2",) foreach ($user in $usersToDisable) { Disable-LocalUser -Name $user } |
In this script, replace “User1”, “User2” with the usernames of the accounts you want to disable. You can add more usernames to the $usersToDisable array if you wish to disable additional accounts.
Script to enable user accounts
1 |
Enable-LocalUser -Name "Username" |
The above PowerShell script uses the Enable-LocalUser cmdlet to disable a user account on Windows devices.
1 2 3 4 5 |
$usersToEnable = @("User1", "User2") foreach ($user in $usersToEnable) { Enable-LocalUser -Name $user } |
In this script, replace “User1”, “User2” with the usernames of the accounts you want to enable. You can add more usernames to the $usersToEnable array to enable additional accounts.
Script to fetch all the users on Windows devices
Admins can use the script below to fetch all the users present on Windows devices. This script can help them manage new user accounts.
1 |
Get-LocalUser |