1

I need to get a plaintext list or array of all the local users on my system so I can loop through them and preform certain actions. By "plaintext" I mean just the user's names. Nothing else. No fancy formatting, no titles, no groups, nothing but the user's names.

I've googled around and tried several solutions, (for e.g. this one powershell - list local users and their groups) but they all have extraneous data that makes looping through the users impossible.

Is there any way I can get just a plain list of users? I wouldn't mind a cmd solution if that's what you have. Note that I have already tried net users, but like I stated earlier, it has this extraneous data.

2
  • Just remove the parts of the info you don't want from the script in your linked answer. It's not difficult. Just remove the information you don't want from the Select-Object list of items.
    – Ken White
    Jul 24, 2014 at 1:54
  • @KenWhite To be quite honest, this is the first time I've ever used PowerShell, and while I have tried to do what you suggest I haven't managed to get it right. I will keep trying though.
    – Seth
    Jul 24, 2014 at 2:02

3 Answers 3

1

VBScript:

Dim ADsContainer, User
Set ADsContainer = GetObject("WinNT://.,Computer")
ADsContainer.Filter = Array("User")
For Each User In ADsContainer
  WScript.Echo User.Name
Next

Paste the above lines into GetLocalUsers.vbs and run it like this:

cscript //nologo GetLocalUsers.vbs

If you want to use WMI directly in PowerShell, you can use this:

get-wmiobject Win32_UserAccount -filter 'LocalAccount=TRUE' |
  select-object -expandproperty Name
3
  • I'm not using VBScript, so I don't see how this will help me.
    – Seth
    Jul 24, 2014 at 3:02
  • You can run VBScript scripts directly in PowerShell. Jul 24, 2014 at 14:42
  • Yes, I'm sure I could, but that wasn't what I wanted to do ;) Your second solution works perfectly though.
    – Seth
    Jul 24, 2014 at 15:18
1

You have to write it like this to get it formatted as you requested:

Get-WmiObject -Class Win32_UserAccount | Format-wide -property name -column 1

Prints:

Administrator
Guest
and so on..
1
  • This is exactly what I was looking for! I wasn't sure how to format the output to just show the names, excluding the categories. I'm going to accept the other answer because it filters the object by local accounts too, just to be on the safe side. Thanks for this though!
    – Seth
    Jul 24, 2014 at 15:17
1

After a bit more experimentation, reading, etc, I found a rather simple solution:

$accounts = Get-WmiObject -Class Win32_UserAccount | Select name

Returns:

name
----
Administrator
Guest
John Doe
Other User  

Not quite what I want, but if I loop through it like this:

foreach ($i in $accounts) { Write-host $i.name }  

It prints:

Administrator
Guest
John Doe
Other User 

This is messy, so I shortened it down to one loop:

foreach ($i in Get-WmiObject -Class Win32_UserAccount | Select name) { 
    # refer to the looped user as $i.name now
}

and per Bill_Stewart's comment, it is a good idea to filter by local account:

foreach ($i in Get-WmiObject -Class Win32_UserAccount -filter 'LocalAccount=true' | Select name) { 
    # refer to the looped user as $i.name now
}

Still less than optimal, but for now it suits my needs.

2
  • 1
    I recommend adding -filter 'LocalAccount=TRUE' to the command to filter out domain accounts in case the computer is on the domain. Also, I don't recommend 'write-host' for your use case because its output cannot be redirected to a file. (See my second answer.) Jul 24, 2014 at 15:05
  • @Bill_Stewart Yes, the write-host was just for testing. I forgot to edit in what I was actually using. Fixed now. Thanks for the filter tip, can't believe I forgot to do that.
    – Seth
    Jul 24, 2014 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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