Back to Resources
Level
Script
Security
This script tackles the often-overlooked issue of unauthorized local administrators on Windows endpoints by automatically detecting any admin accounts that are not officially approved, preventing security gaps that can arise from unnoticed privileged users.
It leverages Level script variables (such as “DetectedAdmins”) alongside custom fields (for example, “AuthorizedAdmins”) to cross-check active Windows admin accounts against your organization’s sanctioned admin list. If any unapproved user is found, the script flags and reports them, then exits with an error code to trigger alerts or additional actions through Level. By integrating with a script-based monitor, you can seamlessly generate notifications whenever an unauthorized admin appears.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
Level Library
https://level.io/library/script-windows-unauthorized-admins
#>
$AuthorizedAdmins = "{{cf_authorized_admins}}"
# Convert both lists to arrays and trim spaces
$detectedAdmins = "{{DetectedAdmins}}"
$detectedArray = $detectedAdmins -split ',' | ForEach-Object { $_.Trim().ToLower() }
$authorizedArray = $AuthorizedAdmins -split ',' | ForEach-Object { $_.Trim().ToLower() }
# Find admins in detected list but not in authorized list
# Convert both to lowercase for case-insensitive comparison
$unauthorizedAdmins = $detectedArray | Where-Object { $authorizedArray -notcontains $_ }
# Output unauthorized admins separated by commas
$unauthorizedString = $unauthorizedAdmins -join ','
if ($unauthorizedAdmins.Count -gt 0) {
Write-Output "$unauthorizedString"
exit 1
} else {
Write-Output "No unauthorized admins detected."
exit 0
}
Windows - Unauthorized Admins
This PowerShell script compares detected administrator accounts against a predefined list of authorized administrators using Level's script variables and custom fields. It processes both lists to create standardized, case-insensitive arrays, then identifies any administrators who are present in the detected list but not in the authorized list. The script outputs either a comma-separated list of unauthorized administrators and exits with an error code, or confirms no unauthorized admins were found and exits successfully.
PowerShell
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.