List all windows UserNames & Domain in VBA

List all User Accounts in Windows using VBA

In vba use this code to get windows user account names & their domain names.

Copy paste this code into your Excel vba project module.

Run the code by pressing F5. You will get a complete list of use account names and additional details in worksheet1.

Sub GetAllUsers()
    Dim iSh As Worksheet
    Dim i As Double
    Set iSh = ThisWorkbook.Sheets(1)
    
    On Error Resume Next
    i = 1
    iSh.Cells(i, 1) = "Account Type"
    iSh.Cells(i, 2) = "Caption"
    iSh.Cells(i, 3) = "Description"
    iSh.Cells(i, 4) = "Disabled"
    iSh.Cells(i, 5) = "Domain"
    iSh.Cells(i, 6) = "Full Name"
    iSh.Cells(i, 7) = "Local Account"
    iSh.Cells(i, 8) = "Lockout"
    iSh.Cells(i, 9) = "Name"
    iSh.Cells(i, 10) = "Password Changeable"
    iSh.Cells(i, 11) = "Password Expires"
    iSh.Cells(i, 12) = "Password Required"
    iSh.Cells(i, 13) = "SID"
    iSh.Cells(i, 14) = "SID Type"
    iSh.Cells(i, 15) = "Status"
    
    sComputerName = "."
    Set oWMIsvc = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & sComputerName & "\root\cimv2")
     
    Set oWMICUserAcctDetails = oWMIsvc.ExecQuery _
        ("Select * from Win32_UserAccount Where LocalAccount = True")
     
    For Each oAcct In oWMICUserAcctDetails
        i = i + 1
        iSh.Cells(i, 1) = oAcct.AccountType
        iSh.Cells(i, 2) = oAcct.Caption
        iSh.Cells(i, 3) = oAcct.Description
        iSh.Cells(i, 4) = oAcct.Disabled
        iSh.Cells(i, 5) = oAcct.Domain
        iSh.Cells(i, 6) = oAcct.FullName
        iSh.Cells(i, 7) = oAcct.LocalAccount
        iSh.Cells(i, 8) = oAcct.Lockout
        iSh.Cells(i, 9) = oAcct.Name
        iSh.Cells(i, 10) = oAcct.PasswordChangeable
        iSh.Cells(i, 11) = oAcct.PasswordExpires
        iSh.Cells(i, 12) = oAcct.PasswordRequired
        iSh.Cells(i, 13) = oAcct.SID
        iSh.Cells(i, 14) = oAcct.SIDType
        iSh.Cells(i, 15) = oAcct.Status
    Next
End Sub

The above code uses Windows Management Instrumentation Command (WMIC). You can use this services to query some of the Windows OS related details.

Luckily this can be used within Vba also.

Here is a complete reference to know more about this routines: Technet Microsoft website

This page has a vbscript code. We converted this code into Vba & also tested it.