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.

Note:


The script is supported on Windows 10 and 11 devices.

Disclaimer:


The sample scripts provided below are adapted from third-party open source sites.

Script to disable a user account

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.

User account disabled after executing the script

Script to disable multiple user accounts

If you want to disable multiple user accounts, you can use the script below.

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

The above PowerShell script uses the Enable-LocalUser cmdlet to disable a user account on Windows devices.

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.

Details of the all the user accounts on the Windows device

Notes:

  • After executing the script to enable a user account, restart the device for the user account to become enabled.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.
  • Sample Script Repository