Skip to main content

Get Local Administrators

Summary

This task lists the local administrators on windows machine and stores the result in a custom field Local Admins List.

Sample Run

Sample Run 1

Sample Run 2

Dependencies

Task Creation

Script Details

Step 1

Navigate to AutomationTasks
step1

Step 2

Create a new Script Editor style task by choosing the Script Editor option from the Add dropdown menu
step2

The New Script page will appear on clicking the Script Editor button:
step3

Step 3

Fill in the following details in the Description section:

  • Name: Get Local Administrators
  • Description: This script lists the local administrators on windows machine and stores the result in a custom field "Local Admins List".
  • Category: Custom

Summary

Script Editor

Click the Add Row button in the Script Editor section to start creating the script
AddRow

A blank function will appear:
BlankFunction

Row 1 Function: PowerShell script

  • Use Generative AI Assist for script creation: False
  • Expected time of script execution in seconds: 900
  • Operating System: Windows
  • Continue on Failure: True
  • PowerShell Script Editor:
using namespace System.DirectoryServices.AccountManagement

#requires -RunAsAdministrator
#requires -Version 5

try {
$ErrorActionPreference = 'Stop'
$adminGroupMembers = New-Object System.Collections.Generic.List[Object]
$localMachinePath = 'WinNT://{0}' -f $env:COMPUTERNAME
$localMachine = New-Object System.DirectoryServices.DirectoryEntry($localMachinePath)
$adminsSID = (New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType]::BuiltinAdministratorsSid, $null)).Value
$localizedAdmin = (New-Object System.Security.Principal.SecurityIdentifier($adminsSID)).Translate([System.Security.Principal.NTAccount]).Value
$localizedAdmin = $localizedAdmin -split '\\|\/' | Select-Object -Last 1
$admGroup = $localMachine.Children.Find($localizedAdmin, 'group')
$adminMembers = $admGroup.Invoke('members', $null)
foreach ($groupMember in $adminMembers) {
$member = New-Object System.DirectoryServices.DirectoryEntry($groupMember)
$username = if ($member.path -match [regex]::escape($env:COMPUTERNAME)) {
($member.path -split '/')[-1]
} else {
$member.path -replace [regex]::escape('WinNT://'), ''
}
$adminGroupMembers.Add([PSCustomObject]@{
Name = $username
})
}
if ($adminGroupMembers.Count -eq 0) {
return 'Result: No local administrator accounts were found.'
}
$adminGroupMembers.Name -join '|'
} catch {
throw ('Result: Error fetching local administrators. Reason: {0}' -f $Error[0].Exception.Message)
}

image4

Row 2 Function: Script Log

  • Script Log Message: %Output%
  • Continue on Failure: False
  • Operating System: Windows

Image5

Step 3 Logic: If/Then

Click on Add Logic > select If/Then

Row 3a Condition: Output Contains

  • Condition: Output
  • Operator: Does not Contain
  • Input Values: Result:

Image6

Row 3b Function: Set Custom Field

  • Select Local Admins List from dropdown
  • Add %output% in the Value

Image7

Save Task

Click the Save button at the top-right corner of the screen to save the script.
SaveButton

Completed Task

Completed Task

Output

  • Script Log

Schedule Task

Task Details

  • Name: Get Local Administrators
  • Description: This script lists the local administrators on windows machine and stores the result in a custom field "Local Admins List".
  • Category: Custom

Image9

Schedule

  • Schedule Type: Schedule
  • Timezone: Local Machine Time
  • Start: <Current Date>
  • Trigger: Time At <Current Time>
  • Recurrence: Every day

Image10

Targeted Resource

Device Group: Machines Opted for Local Admin Detection

Image11

Completed Scheduled Task

Image12

Changelog

2026-03-26

  • Updated the script to function independently of the system's OS language settings.

2026-01-30

  • Initial version of the document