List All User Accounts on a Windows System via Command Line

7

3

I would like a command to list all of the user accounts in a Windows (Vista, 7, etc.) system in a way that I can use to iterate through them with a subsequent command.

net user gives me the data for which I'm searching, but it adds a bunch of other junk that would cause difficulty in parsing the users.

Ideally, I would receive output like:

> usercommand
user1
user2
user3

palswim

Posted 2013-06-18T06:31:41.060

Reputation: 2 793

Answers

8

If you want to iterate through users strictly in the Windows command line, the easiest way would be a combination of wmic and a for loop.

for /f "tokens=* skip=1" %%a in ('wmic UserAccount get Name') do (
    if not "%%a"=="" (
        :: %%a is a variable containing an account name
    )
)

The heart of the command is wmic UserAccount get Name, which should print out a list of accounts. You may wish to do some filtering, like Karan did in his VBScript answer, with something like wmic UserAccount where "LocalAccount=True" get Name. Any field is filterable; to view all of them, use wmic UserAccount get (omitting Name).

The for loop is simply used to parse the command output. It skips the first line (which prints the column heading), and the last line is skipped with the if command, since it is empty. See for /? for more information.

Bob

Posted 2013-06-18T06:31:41.060

Reputation: 51 526

2Beware that if you're in a domain (at least in a Samba domain, don't know about Windows AD), wmic will also list all domain accounts which might not be what you want, and also takes quite some time. wmic useraccount where "localaccount=true" get name prevents this. – Martin von Wittich – 2016-11-15T13:43:09.863

10

For anyone who is here just looking for a way to list all users on your machine in the command line, and don't need a loop. Just run this command:

net user

And it will output what you need in this format

-------------------------------------
User1    User2    User3    User4
The command completed successfully.

trueCamelType

Posted 2013-06-18T06:31:41.060

Reputation: 429

1This info is already in the question. – CristiFati – 2018-10-02T15:40:12.697

That's true, but I put it here for all of the people that were scrolling straight to the answers. It's a common question, and this was the number one search result for how to list all users on a machine in the command line at the time I wrote it. Reading the questions is important though. – trueCamelType – 2018-10-02T17:57:39.140

0

This will output literally what you're asking for:

dir /b C:\Users

Guest

Posted 2013-06-18T06:31:41.060

Reputation: 1

That will fail to list any accounts that have not yet logged into the particular machine. – palswim – 2019-02-25T19:13:42.117

0

This Windows PowerShell script will provide a list of users in a table format, it's not exactly what you are looking for but it shouldn't be too hard to reformat the output into a format you could use to feed into another command.

$computerName = "$env:computername"
$computer = [ADSI]"WinNT://$computerName,computer" 
$computer.psbase.Children | Where-Object { $_.psbase.schemaclassname -eq 'user' } | Format-Table Name, Description -autoSize 

Richard Lucas

Posted 2013-06-18T06:31:41.060

Reputation: 2 744

Inspired by this script: http://powershell.com/cs/media/p/2327.aspx

– Richard Lucas – 2013-06-18T07:33:33.817

0

  1. Save the following with a name like GetLocalUsers.vbs:

    Set colItems = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_UserAccount Where LocalAccount=True")
    For Each objItem in colItems
        Wscript.Echo objItem.Name
    Next
    
  2. Run from the command line as follows:

    cscript //NoLogo GetLocalUsers.vbs
    

Karan

Posted 2013-06-18T06:31:41.060

Reputation: 51 857