Use PowerShell to Get a List of Users on Windows 7

You can use PowerShell to get a list of local workstation user accounts on Windows 7 or Windows 10. (Probably Windows 8 and 8.1, but does anybody really care?)

This is much easier on Windows 10, than on 7.

Get-LocalUser | Select Name

Not much to write about there, but most of us still have some Windows 7 computers floating around, and that won’t work there. You can use net user, but it’s output is kind of useless.

PS C:\Windows\system32> net user

User accounts for \\MYCOMPUTER

--------------------------------------------------
Administrator       User               Guest
testuser1           testuser2     
The command completed successfully.

This little script will transform net user‘s ugly output into a more useful array:

#Put the output of net user into a variable. 
#PowerShell automatically sets this as an array since 
#the output is multi-lined.
$NetUser = (net user)
#Count how many lines are output by net user.
$LineCount = ($NetUser | Measure).Count
#Discount the last 3 lines of net user,
#since they're just noise.
$Count = $LineCount-3
#Cycle through all of the lines of $NetUser.
do {
    #Split the current line of $NetUser on the space 
    #character into a separate array
    $LineList = $NetUser[$Count].split(' ')
    #Remove all of the blank items from the new array.
    $UserList += $LineList -ne ""
    #Increment the count.
    $Count -= 1
}
#Exclude the first four lines (the count starts with 
#zero), because they are also noise.
while ($Count -gt 3)

Leave a Reply

Your email address will not be published. Required fields are marked *