PoshCode Logo PowerShell Code Repository

Get Local Security Groups and their Members from Remote Computers.

This was modified from Wright47’s 07 May 2009 12:58 AM post, and Ying Li’s post from 8/31/2007

  1. #You'll need admin perms on all the servers to run this...
  2. Write-Host "ComputerName in White, then Local Groups in Green, Local Groups' Members in White"
  3.  
  4.  
  5. #This feeds the script a list of server names (ServerList.txt, placed in the same directory)
  6. #for which I want the Local Groups and all those groups' Members.
  7.  
  8.  
  9. $x = Get-Content ServerList.txt
  10.  
  11. # this feeds each servername, one by one, into the process...
  12.  
  13. foreach ($j in $x) {
  14.  
  15.  
  16. #The server name becomes variable $strComputer
  17.  
  18. $strComputer = "$j"
  19.  
  20.  
  21. #The Server name then becomes the variable $strComputer, and its name
  22. #is printed out in all CAPS
  23.  
  24. $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
  25. $g = $computer.name.ToString()
  26. $g.ToUpper(); " "
  27.  
  28.  
  29. #The local security groups on the Server is then found,
  30. #and fed to the array $a
  31.  
  32. $group = $computer.psbase.children | where{$_.psbase.schemaclassname -eq "group"}
  33. $a = @()
  34. foreach ($member in $group.psbase.syncroot)
  35. {$a += $member.name }
  36.  
  37.  
  38.  
  39. #Each Local Group Member is then printed (in white,and with a space) underneath its Group's name
  40. #(Group name printed in green)
  41.  
  42. foreach ($i in $a) {
  43.  
  44. $group =[ADSI]"WinNT://$j/$i"
  45. Write-Host "$i" -fore green
  46. $members = @($group.psbase.Invoke("Members"))
  47. $members | foreach { Write-Host " ",$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
  48. }
  49.  
  50. Write-host " "
  51. Write-host " "
  52. Write-host " "
  53. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me