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
Best Answer
If you want to iterate through users strictly in the Windows command line, the easiest way would be a combination of
wmic
and afor
loop.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 likewmic UserAccount where "LocalAccount=True" get Name
. Any field is filterable; to view all of them, usewmic UserAccount get
(omittingName
).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 theif
command, since it is empty. Seefor /?
for more information.