«

»

Oct 01

How to: List members of the Administrators local group

This is a simple powershell script that will list all the members in the Administrators local group. Just add all the computer name in C:\Servers.txt.

$Computers = Get-Content "C:\servers.txt"
foreach ($Computer in $Computers) {
    $strcomputer = [ADSI]("WinNT://" + $Computer + ",computer")
    $Group = $strcomputer.psbase.children.find("Administrators")
    $members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
    Write-Host $Computer
    Write-Host "*********************"
    ForEach($user in $members){
        Write-Host $user
    }
    Write-Host ""
}

If you have idea to enhance that script please feel free to post it in the comments section.

4 comments

Skip to comment form

  1. TK

    Hi,

    Is there a simple way to direct the output to a file? Getting a long list this way is not ideal…

  2. Dan Fortier

    Hey,

    You could replace Write-host by add-content, here’s how you would do it:

    Replace:

    Write-Host $Computer

    By:

    $Computer | Add-Content C:\export.txt

    With Add-content you need to concatenate your sting if you have text and variables:

    "*********" + $Computer + "************" + $User | Add-Content C:\export.txt
  3. Dean

    THis is brilliant, works a treat, shame it took me so long to find your page when looking for this. You’re now in my favourites :)

    Thank you very much!!!

  4. Tim

    Hi Dan,

    I like this list as I can export it to a CSV , but I would like to include the domain (multiple domains) as well infront of the user or group.

    Thanks,

    Tim

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>