Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

net user would give 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
share|improve this question

3 Answers 3

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.

share|improve this answer

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 
share|improve this answer
    
Inspired by this script: powershell.com/cs/media/p/2327.aspx –  Richard Jun 18 '13 at 7:33
  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
    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.