# 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](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) on your Windows devices to delete any specific user account or all user accounts except the specified ones.

 Disclaimer:The Sample Scripts provided below are adapted from third-party Open-Source sites.

 

Batch script to delete a specific user account
----------------------------------------------

Batch script to delete a specific user account

net user %1 /delete 

   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 
----------------------------------------------------

PowerShell script to delete a specific user account

param(\[string\] $user\_name="") net user $user\_name /delete 

   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
---------------------------------------------------------------------------

PowerShell script to delete all user accounts except specific ones

$UserList = @("Administrator","DefaultAccount","Guest","IEUser","sshd","WDAGUtilityAccount") Get-WMIObject -Class Win32\_UserAccount | Where-Object {$\_.Name -notin $UserList} | Foreach {net user $\_.Name /delete}

   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*:

PowerShell script to delete all user accounts except specific ones

$UserList = @("DefaultAccount","Guest","IEUser","sshd") Get-WMIObject -Class Win32\_UserAccount | Where-Object {$\_.Name -notin $UserList} | Foreach {net user $\_.Name /delete}

   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.

 Notes:- 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.