Jump to content

Get List of All Windows Users..??


Recommended Posts

Just threw this together...

$WinTitle = "User Accounts"

Run("control userpasswords2")
WinWait($WinTitle)
$count = ControlListView($WinTitle, "", 1001, "GetItemCount") ; count users
Local $UserList[$count] ; create array

For $i = 0 To $count-1
    $text = ControlListView($WinTitle, "", 1001, "GetText", $i) ; get username
    $UserList[$i] = $text ; add to array
    ConsoleWrite($text & @CRLF); display in console (for testing)
Next

WinClose($WinTitle) ; Close User Accounts Window

Msgbox(64, "Users", "There are: " & UBound($UserList) & " user accounts")
For $i = 0 to UBound($UserList)-1 Step +1
    MsgBox(64, "User " & $i+1, $UserList[$i])
Next

Steve

EDIT: Code Updated.

Edited by Steveiwonder

They call me MrRegExpMan

Link to comment
Share on other sites

You can use this way too.

But it requires Administrative Privilegies.

#RequireAdmin
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
Dim $avarray
RunWait("cmd.exe /c " & "wmic USERACCOUNT get Name >"&@Tempdir&"\users.txt",@ScriptDir,@SW_HIDE)

_FileReadToArray(@Tempdir&"\users.txt",$avarray)


$Form1 = GUICreate("TEST GET USER LIST", 398, 217, -1,-1)
$Combo1 = GUICtrlCreateCombo("", 96, 48, 185, 25, $CBS_DROPDOWNLIST)
For $i=2 To $avarray[0]
    GUICtrlSetData($Combo1,$avarray[$i])
    Next

$Button1 = GUICtrlCreateButton("Ok", 136, 152, 89, 25, $WS_GROUP)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1

MsgBox(64,"",GUICtrlRead($Combo1),15)



    EndSwitch
WEnd
[size="5"] [/size]
Link to comment
Share on other sites

Interesting choices..

How about enumerating all subkeys under this?:

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
I think the ones starting with S-1-5-21 are user profiles, correct?

*edit: nevermind, looks like there's a bit more to SID's than I thought:

"Well-known security identifiers in Windows operating systems"

Edited by Ascend4nt
Link to comment
Share on other sites

Try it:

#include <Array.au3>

$Users = _SystemUsers(0)
_ArrayDisplay($Users)

#cs ===============================================================================
    Function:      _SystemUsers($AccountType = 0)
    Description:   Return an array with the local or domain username
    Parameter(s):  $AccountType: Local, domain or both username
        0 = Local and Domain usernames
        1 = Local usernames only
        2 = Domain usernames only
    Returns:       An array with the list of usernames - Succeeded
        @error 1 - Didn't query any username
        @error 2 - Failed to create Win32_SystemUsers object
        @error 3 - Invalid $AccountType

    Author(s):  Danny35d
#ce ===============================================================================
Func _SystemUsers($AccountType = 0)
    Local $aSystemUsers
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $strComputer = "localhost"

    If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '')
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $Output = StringSplit($objItem.PartComponent, ',')
            If IsArray($Output) Then
                $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '')
                If $AccountType = 0 Or ($AccountType = 1 And @ComputerName = $Temp) Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                ElseIf $AccountType = 2 And @ComputerName <> $Temp Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                EndIf
            EndIf
        Next
        $aSystemUsers = StringTrimRight($aSystemUsers, 1)
        If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers))
        Return(SetError(0, 0, StringSplit($aSystemUsers, '|')))
    Else
        $aSystemUsers = ''
        Return(SetError(2, 2, $aSystemUsers))
    EndIf
EndFunc   ;==>_SystemUsers
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

  • 2 years later...

Try it:

#include <Array.au3>

$Users = _SystemUsers(0)
_ArrayDisplay($Users)

#cs ===============================================================================
    Function: _SystemUsers($AccountType = 0)
    Description: Return an array with the local or domain username
    Parameter(s): $AccountType: Local, domain or both username
        0 = Local and Domain usernames
        1 = Local usernames only
        2 = Domain usernames only
    Returns: An array with the list of usernames - Succeeded
        @error 1 - Didn't query any username
        @error 2 - Failed to create Win32_SystemUsers object
        @error 3 - Invalid $AccountType

    Author(s): Danny35d
#ce ===============================================================================
Func _SystemUsers($AccountType = 0)
    Local $aSystemUsers
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $strComputer = "localhost"

    If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '')
    $objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $Output = StringSplit($objItem.PartComponent, ',')
            If IsArray($Output) Then
                $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '')
                If $AccountType = 0 Or ($AccountType = 1 And @ComputerName = $Temp) Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                ElseIf $AccountType = 2 And @ComputerName <> $Temp Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                EndIf
            EndIf
        Next
        $aSystemUsers = StringTrimRight($aSystemUsers, 1)
        If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers))
        Return(SetError(0, 0, StringSplit($aSystemUsers, '|')))
    Else
        $aSystemUsers = ''
        Return(SetError(2, 2, $aSystemUsers))
    EndIf
EndFunc ;==>_SystemUsers

I know this is an old thread now, but this is a nice script. But sadly i can't get

AccountType to work.

If i try to put in 0, 1 or even 2 it always write out 0.

Any tips?

Description: Return an array with the local or domain username

Parameter(s): $AccountType: Local, domain or both username

0 = Local and Domain usernames

1 = Local usernames only

2 = Domain usernames only

Link to comment
Share on other sites

I got it sorted out, there was a bug in your code. $user = systemusers(0). There should not be a 0 in that container, it must be empty for the $accounttype = 0/1/2 to work.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>
#include <File.au3>
Dim $AccountType
$sFile = @ScriptDir & "users.txt"
$Users = _SystemUsers()
_FileListToArray($Users)
$hFile = FileOpen($sFile, 2) ; 1 = append 2 = empty file
_FileWriteFromArray($hFile,$Users, 1)
FileClose($hFile)

#cs ===============================================================================
    Function:     _SystemUsers($AccountType = 0)
    Description:   Return an array with the local or domain username
    Parameter(s):  $AccountType: Local, domain or both username
        0 = Local and Domain usernames
        1 = Local usernames only
        2 = Domain usernames only
    Returns:       An array with the list of usernames - Succeeded
        @error 1 - Didn't query any username
        @error 2 - Failed to create Win32_SystemUsers object
        @error 3 - Invalid $AccountType
    Author(s):  Danny35d
#ce ===============================================================================
Func _SystemUsers($AccountType=1)
    Local $aSystemUsers
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $strComputer = "localhost"
    If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '')
    $objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $Output = StringSplit($objItem.PartComponent, ',')
            If IsArray($Output) Then
                $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '')
                If ($AccountType = 0) Or ($AccountType = 1 And @ComputerName = $Temp) Then
     ;MsgBox(4096, "FEIL1", $AccountType , 5) ; Mulighet for testing av om hvilken løkke som slår inn
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                ElseIf ($AccountType = 2 And @ComputerName <> $Temp) Then
     ;MsgBox(4096, "FEIL2", $AccountType , 5) ; Mulighet for testing av om hvilken løkke som slår inn
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                EndIf
            EndIf
        Next
        $aSystemUsers = StringTrimRight($aSystemUsers, 1)
        If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers))
        Return(SetError(0, 0, StringSplit($aSystemUsers, '|')))
    Else
        $aSystemUsers = ''
        Return(SetError(2, 2, $aSystemUsers))
    EndIf
EndFunc   ;==>_SystemUsers
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...