Windows admin PowerShell

Export the members of the local Administrators group

List the members of the local Administrators group on one or more computers and save them to a CSV file for an access review.

LanguagePowerShell
What it does to the systemReads only. It changes nothing.

Auditors ask who has administrator rights on your computers. Get-LocalGroupMember can fail with an error when the group holds an account that no longer exists.

This script reads the group through the WinNT provider. It lists users and groups, including leftover accounts, and can write a CSV file.

PowerShell
<#
  Members of the local Administrators group on one or more computers.
  Remote computers need administrator rights and a network path to the computer (port 445).
  Example: .\Get-LocalAdmin.ps1 -ComputerName PC01, PC02 -CsvPath C:\Temp\admins.csv
#>
param(
    [string[]]$ComputerName = $env:COMPUTERNAME,

    # The name of the group. Change it on Windows in another language.
    [string]$GroupName = 'Administrators',

    # Optional: also save the result to this CSV file.
    [string]$CsvPath = ''
)

$rows = foreach ($computer in $ComputerName) {
    try {
        $group = [ADSI]"WinNT://$computer/$GroupName,group"
        foreach ($member in @($group.Invoke('Members'))) {
            # Members are COM objects. Read their properties this way.
            $adsPath = $member.GetType().InvokeMember('ADsPath', 'GetProperty', $null, $member, $null)
            $class = $member.GetType().InvokeMember('Class', 'GetProperty', $null, $member, $null)
            [pscustomobject]@{
                ComputerName = $computer
                Member       = ($adsPath -replace '^WinNT://', '') -replace '/', '\'
                Type         = $class
                Error        = ''
            }
        }
    }
    catch {
        [pscustomobject]@{ ComputerName = $computer; Member = ''; Type = ''; Error = $_.Exception.Message }
    }
}

if ($CsvPath) { $rows | Export-Csv -LiteralPath $CsvPath -NoTypeInformation -Encoding UTF8 }
$rows

How to use it

  1. Save the script as Get-LocalAdmin.ps1.
  2. Run .\Get-LocalAdmin.ps1 to test it on this computer.
  3. Add -ComputerName (Get-Content C:\Temp\computers.txt) for a list of computers.
  4. Add -CsvPath C:\Temp\admins.csv to save the result.
  5. On Windows in another language, set -GroupName to the local name of the group.

Good to know

  • A member that shows as a SID, such as S-1-5-21-..., is an account that was deleted. Remove it from the group.
  • The script does not open domain groups. A domain group in the list means all its members are administrators.
local administrators group members ADSI WinNT access review
✉

Get new articles in your inbox

SCCM guides, AI tools, automation tips — sent when published. No spam.