PowerShell - Get ALL users from a text list of servers
- Jon Boyette
- Jan 10, 2022
- 1 min read
Save as a .ps1, or a copy and paste, populate c:\temp\servers.txt, with servers you want to know what users are on each, output to c:\temp\RemoteLocalUsers.csv
get-content C:\temp\servers.txt | foreach-object {
$Comp = $_
if (test-connection -computername $Comp -count 1 -quiet)
{
([ADSI]"WinNT://$comp").Children | ?{$_.SchemaClassName -eq 'user' } | %{
$groups = $_.Groups() | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
$_ | Select @{n='Computername';e={$comp}},
@{n='UserName';e={$_.Name}},
@{n='Active';e={if($_.PasswordAge -like 0){$false} else{$true}}},
@{n='LastLogin';e={$_.LastLogin}},
@{n='Memberof';e={$groups -join ';'}}
}
} Else {Write-Warning "Server '$Comp' is Unreachable hence Could not fetch data"}
}|Export-Csv -NoTypeInformation c:\temp\RemoteLocalUsers.csv
Comments