📱 Recipe · Intune & Device Management
Create an Intune Remediation Script to Detect Unauthorized Local Administrators
Deploy a PowerShell detection script via Intune Scripts and Remediations to surface abnormal local admin accounts across your Windows endpoint fleet
Complexity
Intermediate
Impact
security + compliance + endpoint-management + least-privilege + audit
Context
Why This Matters
Over time, Windows endpoints accumulate local administrator accounts — added by field technicians for break-fix work, by legacy imaging processes, or by users who briefly needed elevation. These accounts are rarely cleaned up, creating a persistent privilege-escalation and lateral-movement risk.
With Windows LAPS and Endpoint Privilege Management (EPM) now handling just-in-time elevation, there is no legitimate reason for unmanaged local admins to persist on corporate devices. This recipe creates an Intune Remediation (Proactive Remediation) that scans the local Administrators group on every targeted device, compares membership against an allowlist, and reports any abnormal accounts directly in the Intune console for review.
Run this when you are migrating from legacy UEM tooling, performing a security baseline audit, or preparing to enforce a least-privilege standard across your estate.
Expected Outcomes
After completing this recipe you will have:
- A tested PowerShell detection script that enumerates local Administrators group members on Windows endpoints
- An Intune Remediation package deployed to a pilot (and then broader) device group
- Per-device reporting in the Intune console showing which endpoints have unauthorized local admins and the account names found
- An exportable CSV of non-compliant devices for follow-up cleanup or automated remediation
- A repeatable detection pattern you can extend for other local-security audits
Risks & Considerations
Warnings and considerations
- Allowlist accuracy is critical. If you omit a legitimate service or LAPS account from the allowlist, every device will flag as non-compliant and generate noise. Inventory your approved accounts before deployment.
- Run as SYSTEM, not user. The script enumerates a privileged local group. Setting Run this script using the logged-on credentials to Yes will cause failures on standard-user devices.
- Domain-joined devices: the script filters to
PrincipalSource -eq 'Local'so domain and Azure AD accounts in the Administrators group are not flagged. Adjust this filter if your policy prohibits those as well. - Do not combine detection + automatic removal on first run. Deploy in detect-only mode, review results, confirm the allowlist is complete, then enable remediation in a second phase.
- Compliance/audit logging: removing local admin accounts may be a change-controlled action. Coordinate with your security and change-management teams before moving beyond reporting mode.
- Intune Remediations license requirement: requires Intune Plan 1 plus one of Windows 10/11 Enterprise E3/E5, Windows 365, or the standalone Remediations add-on.
Required Permissions
| Permission | Why It's Needed |
|---|---|
| Intune Administrator or Endpoint Security Manager role | Required to create and assign Scripts and Remediations in the Intune admin center |
| DeviceManagementConfiguration.ReadWrite.All | Required when creating or updating deviceHealthScripts via Microsoft Graph API |
| DeviceManagementManagedDevices.Read.All | Required to review remediation script run states and per-device output via Graph |
| Local SYSTEM execution context on target devices | The detection script must run as SYSTEM to enumerate the local Administrators group reliably |
The fastest way to get this done: just ask Dex. Copy the prompt below and paste it into your Dex conversation.
For IT Admins
Paste into Dex Pro
Create the remediation package in the Intune admin center
- Sign in to the Microsoft Intune admin center at
https://intune.microsoft.comwith an Intune Administrator account. - In the left navigation, go to .
- Select the Remediations tab, then click + Create.
- On the Basics page enter:
- Name:
Detect Unauthorized Local Administrators - Description: Reports local accounts in the Administrators group that are not on the approved allowlist.
- Publisher: your IT team name
- Name:
- On the Settings page:
- Detection script file: upload
Detect-LocalAdmins.ps1(provided below). - Remediation script file: leave blank for report-only, or upload a removal script if you are ready to auto-remediate.
- Run this script using the logged-on credentials:
No(must run as SYSTEM). - Enforce script signature check:
No(unless you code-sign the script). - Run script in 64-bit PowerShell:
Yes.
- Detection script file: upload
- On the Scope tags page, apply any scope tags required by your RBAC model, then Next.
- On the Assignments page, assign to a pilot Azure AD device group first (for example,
Intune-Pilot-Devices). Set the schedule to Daily at a low-impact hour. - Review the summary and click Create.
Review detection results
- After the next check-in cycle (typically within a few hours), return to .
- Open the Device status tab to see per-device results.
- Add the column Pre-remediation detection output to see the list of abnormal accounts reported by each device.
- Click Export to download a CSV of non-compliant devices for follow-up.
Create a deviceHealthScript via Microsoft Graph
Intune Remediations are exposed in Graph as deviceHealthScripts. The detection and remediation script bodies must be Base64-encoded UTF-8.
1. Encode the detection script
# In PowerShell
$detectionBytes = [System.IO.File]::ReadAllBytes("./Detect-LocalAdmins.ps1")
$detectionB64 = [System.Convert]::ToBase64String($detectionBytes)2. POST the script definition
POST https://graph.microsoft.com/beta/deviceManagement/deviceHealthScripts
Authorization: Bearer <access_token>
Content-Type: application/json
{
"displayName": "Detect Unauthorized Local Administrators",
"description": "Reports local accounts in the Administrators group not on the allowlist.",
"publisher": "IT Security",
"runAsAccount": "system",
"enforceSignatureCheck": false,
"runAs32Bit": false,
"detectionScriptContent": "<base64-of-Detect-LocalAdmins.ps1>",
"remediationScriptContent": ""
}3. Assign the script to a device group
POST https://graph.microsoft.com/beta/deviceManagement/deviceHealthScripts/{id}/assign
{
"deviceHealthScriptAssignments": [
{
"target": {
"@odata.type": "#microsoft.graph.groupAssignmentTarget",
"groupId": "<pilot-device-group-objectId>"
},
"runRemediationScript": false,
"runSchedule": {
"@odata.type": "#microsoft.graph.deviceHealthScriptDailySchedule",
"interval": 1,
"time": "03:00:00",
"useUtc": true
}
}
]
}4. Retrieve per-device run results
GET https://graph.microsoft.com/beta/deviceManagement/deviceHealthScripts/{id}/deviceRunStates?$expand=managedDeviceThe preRemediationDetectionScriptOutput property on each run state contains the list of abnormal account names reported by that device.
Using the detection script
The detection script below is what Intune runs on every targeted endpoint. It:
- Resolves the localized name of the built-in Administrators group via its well-known SID
S-1-5-32-544(required for non-English Windows installs). - Enumerates the group using
Get-LocalGroupMember. - Filters to
PrincipalSource -eq 'Local'so domain / Azure AD accounts are ignored. - Compares remaining accounts against the
$allowedAccountsallowlist. - Exits
1with a descriptive message when abnormal accounts exist (Intune reports Issue detected), or0when the device is clean.
Before deploying
- Edit the
$allowedAccountsarray to reflect your approved accounts — typically the renamed built-in Administrator, your LAPS managed account, and any sanctioned break-glass accounts. - Test locally by running the script in an elevated PowerShell session on a reference device:
powershell.exe -ExecutionPolicy Bypass -File .\Detect-LocalAdmins.ps1; echo $LASTEXITCODE - Confirm the exit code matches expectations (0 = clean, 1 = flagged).
- Upload the validated script to the Intune Remediation package created above.
Extending to auto-remediation (phase 2)
Once your allowlist is proven accurate, pair the detection with a remediation script that calls Remove-LocalGroupMember -Group $groupName -Member $accountName for each flagged account. Deploy to a small ring first and review the Post-remediation detection output column before broadening.
Next steps