Local accounts, groups and members
Win32_UserAccount and Win32_Group describe accounts and groups. On a domain-joined machine these classes also enumerate the domain: the LocalAccount = TRUE filter limits the query to local objects. The Win32_GroupUser association gives the members of each group.
$accountFilter = 'LocalAccount = TRUE'
Get-CimInstance -ClassName Win32_UserAccount -Filter $accountFilter |
Select-Object -Property Name, FullName, Disabled, Lockout, PasswordRequired, SID |
Sort-Object -Property Name
$group = Get-CimInstance -ClassName Win32_Group -Filter $accountFilter
foreach ($localGroup in $group) {
$member = Get-CimAssociatedInstance -InputObject $localGroup -Association Win32_GroupUser
[PSCustomObject]@{
Group = $localGroup.Name
Member = ($member | ForEach-Object -Process { '{0}\{1}' -f $_.Domain, $_.Name }) -join ', '
}
}
The first query lists the accounts; the loop lists the groups with their members on one line. Get-CimAssociatedInstance starts from the group and follows Win32_GroupUser to the members (accounts or other groups). If the group holds domain accounts, resolution can be slow.
Variant: remote
Members of the administrators group of a server. On a system in another language, the group has a localized name: adapt it in the filter.
$cimSession = New-CimSession -ComputerName 'SERVER01'
try {
$cimParameters = @{
CimSession = $cimSession
ClassName = 'Win32_Group'
Filter = "Name = 'Administrators'"
}
$group = Get-CimInstance @cimParameters
Get-CimAssociatedInstance -InputObject $group -Association Win32_GroupUser |
Select-Object -Property Domain, Name
} finally {
Remove-CimSession -CimSession $cimSession
}