# Script to list Windows local user accounts

Administrators need to keep tab on users accounts in their corporate devices for effective user management. Hexnode users can [deploy scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) to get the details of all the users on a Windows device. Deploying scripts is one of the useful methods to view user details of accounts from Windows devices located remotely.

 Note: 
**Supported Versions**:

The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

 

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

 

Batch Script
------------

Batch script to list Windows local user accounts

@echo off REM List only enabled local user accounts using net user echo Local user accounts: for /f "skip=4 tokens=\*" %%a in ('net user') do ( REM Stop when we reach the footer line echo %%a | findstr /I "The command completed successfully." >nul if errorlevel 1 ( for %%b in (%%a) do ( REM Check if this is a valid account name net user "%%b" >nul 2>&1 if not errorlevel 1 ( net user "%%b" | findstr /I "Account active Yes" >nul if not errorlevel 1 ( echo %%b ) ) ) ) ) echo Listed all enabled local user accounts successfully. exit /b 0

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

  @echo off 

REM List only enabled local user accounts using net user 

echo Local user accounts:

for /f "skip=4 tokens=\*" %%a in ('net user') do (

 REM Stop when we reach the footer line 

 echo %%a | findstr /I "The command completed successfully." >nul 

 if errorlevel 1 (

 for %%b in (%%a) do (

 REM Check if this is a valid account name 

 net user "%%b" >nul 2>&1

 if not errorlevel 1 (

 net user "%%b" | findstr /I "Account active Yes" >nul 

 if not errorlevel 1 (

 echo %%b

 )

 )

 )

 )

)

echo Listed all enabled local user accounts successfully.

exit /b 0

   

 

  

![Batch script to list Windows local user accounts remotely executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/09/Listed-Windows-local-user-accounts-remotely-with-Batch-script--300x148.png)

PowerShell Script
-----------------

PowerShell script to list Windows local user accounts

\# List all enabled local user accounts try { $users = Get-LocalUser | Where-Object { $\_.Enabled -eq $true -and $\_.PrincipalSource -eq 'Local' } if ($users) { Write-Output "Local user accounts:" foreach ($u in $users) { Write-Output $u.Name } } else { Write-Output "No enabled local user accounts found." } } catch { Write-Error "An error occurred: $($\_.Exception.Message)" }

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  \# List all enabled local user accounts 

try {

 $users = Get-LocalUser | Where-Object { $\_.Enabled -eq $true -and $\_.PrincipalSource -eq 'Local' }

 if ($users) {

 Write-Output "Local user accounts:"

 foreach ($u in $users) {

 Write-Output $u.Name

 }

 } else {

 Write-Output "No enabled local user accounts found."

 }

} catch {

 Write-Error "An error occurred: $($\_.Exception.Message)"

}

   

 

  

![PowerShell script to list Windows local user accounts remotely executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/09/Listed-Windows-local-user-accounts-remotely-with-PowerShell-script--300x148.png)

How to View Script Output in Hexnode
------------------------------------

To review the execution results, navigate to the **Action History** tab of the specific device in your Hexnode UEM portal. Locate the script entry in the **Subject** column and click the **Show Output** button next to the status field to view the returned data.

 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.