The following function uses the ADSI provider and some tricks to determine the members of the local Administrators group on any of your Windows Servers. Check it out:

function Get-LocalAdmins
{
param([string]$serverName)
 $ErrorActionPreference = “Stop”
 If ($serverName -eq “”)
 {
  Write-Host “Usage: Get-LocalAdmins “`
  -ForegroundColor yellow
 }
 Else
 {
 $Group = [ADSI]”WinNT://$serverName/Administrators”
 $Memberlist = @($Group.PSBase.Invoke(“Members”))
 $Members = $Memberlist |
  %{$_.GetType().InvokeMember(“Name”,’GetProperty’,$null,$_,$null)}
 Return $Members
 }
}

You use it simply by typing Get-LocalAdmins MYSERVER. Ain’t that neat?

4 Responses to Helpful Function of the Day: Get-LocalAdmins

  1. Sumit GUpta says:

    Hi,

    I am new to the scripting.Could you please let me know how to run this function?

    Thanks,
    Sumit

  2. admin says:

    1. Save it as Get-LocalAdmins.ps1
    2. Start Powershell
    3. Dot-source the script by typing the full path to the script, preceeded by a dot and a space
    4. Run Get-LocalAdmins YOURSERVERNAME
    Hugo

  3. Sumit Gupta says:

    Thanks…it works…

  4. admin says:

    UPDATE: Be advised that this command might generate warning events in the System Event Log with Event-ID 2510: The server service was unable to map error code 1722. Other sources on the internet will tell you this error code means The RPC Server was unavailable. I believe this error message is benign. If anybody can give more insight on this, please add a comment.
    Hugo

Leave a Reply