How To Get A List Of All Local Administrators

Least privilege access is a standard and basic security principle, but I found very important to verify that there are no exceptions or weaknesses in the systems with any unwanted user member of the local administrator group.

This script retrieves a list of all users that are a member of the local administrator group.

function Get-LocalAdmin {  

    <#
        
        .Synopsis
            Get Local admin list
        .Description 
            Get Local admin list

        .Example
            Get-LocalAdmin  -Computername myworkstation.contoso.com

            This shows the NTP Status of the localhost, this will be the result:

            Retrieving Local Admin list for myworkstation.contoso.com
            MYWORKSTATION\Administrator           
            CONTOSO\Domain Admins

        .Example 
             get-adcomputer -searchbase ‘OU=workstations,dc=contoso,dc=com’ -filter * -property * | select name  | Get-LocalAdmin

             Get Local admin list for all the workstation in AD.

        .Notes
              Author: Paolo Frigo - https://www.scriptinglibrary.com

 #>
    param (

        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [Alias('Name')]

        [string[]]$ComputerName
    )             
    Process {
           
        Write-Warning "Retrieving Local Admin list for $ComputerName"   

        try {
            If (!(Test-Connection -ComputerName $computerName -Count 1 -Quiet)) { 
                Write-Output "$computerName is offline."
                #Continue # Move to next computer
            }
            else {
                $admins = Gwmi win32_groupuser –computer $ComputerName 
                $admins = $admins |? {$_.groupcomponent –like '*"Administrators"'}    
                $admins | % {  
                    $_.partcomponent –match “.+Domain\=(.+)\,Name\=(.+)$” > $nul  
                    $matches[1].trim('"') + “\” + $matches[2].trim('"')  }
            }
            
        }
        catch {
            Write-Output "Can't gather information from $ComputerName"
            Write-Output $Error[0].Exception;
        }
        finally {
           
        } 
      
    }
}


#Example with desktop, but you can use Laptops or VMs or Servers as OU
get-adcomputer -searchbase ‘OU=workstations,dc=contoso,dc=com’ -filter * -property * | select name  | Get-LocalAdmin

It simple to use MMC and adding the snap-in of local user and groups for a remote or local machine (or run lusrmgr.msc). But I think this script can be extremely useful to run this check against a large number of workstations.

Note if you’re using Powershell 5.1 or later versions you can use this cmd-let : Get-LocalGroupMember 

get-localgroupmember administrators

ObjectClass Name PrincipalSource
----------- ---- ---------------
User WS\Administrator Local

 

From command prompt you can use also:

net localgroup administrators
Alias name     administrators
Comment        Administrators have complete and unrestricted access to the computer/domain

Members

-------------------------------------------------------------------------------
Administrator
The command completed successfully.

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.