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.

Community
  • 1
  • 1
Seth
  • 528
  • 3
  • 16
  • 32
  • 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 '14 at 01: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 '14 at 02:02

3 Answers3

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
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
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.

Seth
  • 528
  • 3
  • 16
  • 32
  • 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.) – Bill_Stewart Jul 24 '14 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 '14 at 15:20
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..
user3316995
  • 100
  • 2
  • 8
  • 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 '14 at 15:17