--- title: "Create an Intune Remediation Script to Detect Unauthorized Local Administrators" slug: "dex-wf-intune-remediation-detect-local-admins" updated: 2026-11-30T16:15:15Z published: 2026-11-30T16:15:15Z canonical: "documentation.sysaid.com/dex-wf-intune-remediation-detect-local-admins" --- > ## Documentation Index > Fetch the complete documentation index at: https://documentation.sysaid.com/llms.txt > Use this file to discover all available pages before exploring further. # Create an Intune Remediation Script to Detect Unauthorized Local Administrators Deploy a PowerShell detection script through Intune Scripts and remediations to surface unexpected local admin accounts across your Windows endpoint fleet. **Complexity:** Intermediate. **Impact:** security + compliance + endpoint-management + least-privilege + audit. ## Why use this workflow 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, which creates a persistent privilege-escalation and lateral-movement risk. With Windows LAPS and Endpoint Privilege Management (EPM) handling just-in-time elevation, unmanaged local admins have little reason to persist on corporate devices. This workflow creates an Intune remediation that scans the local `Administrators` group on every targeted device, compares membership against an allowlist, and reports unexpected accounts in the Intune admin center for review. Run this when you migrate from legacy endpoint management tooling, perform a security baseline audit, or prepare to enforce a least-privilege standard across your estate. ## Expected outcomes ### After completing this workflow 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 device group (and then a broader group) - Per-device reporting in the Intune admin center 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 and considerations ### Warnings and considerations - **Allowlist accuracy is critical.** If you omit a legitimate service or LAPS account from the allowlist, every device flags as non-compliant and generates noise. Inventory your approved accounts before deployment. - **Run as SYSTEM, not as the user.** The script enumerates a privileged local group. Setting *Run this script using the logged-on credentials* to **Yes** causes failures on standard-user devices. - **Domain-joined devices:** The script filters to `PrincipalSource -eq 'Local'`, so domain and Entra ID accounts in the Administrators group are not flagged. Adjust this filter if your policy prohibits those as well. - **Do not combine detection and automatic removal on the first run.** Deploy in detect-only mode, review results, confirm the allowlist is complete, then enable remediation in a second phase. - **Compliance and audit logging:** Removing local admin accounts may be a change-controlled action. Coordinate with your security and change-management teams before you move beyond reporting mode. - **License requirement:** Intune remediations require specific licensing. Check Microsoft's current licensing requirements for Remediations before you deploy. ## 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 through Microsoft Graph | | DeviceManagementManagedDevices.Read.All | Required to review remediation script run states and per-device output through Graph | | Local SYSTEM execution context on target devices | The detection script must run as SYSTEM to enumerate the local Administrators group reliably | ## Ask Dex Paste the prompt into a Dex conversation. ### Prompt for IT admins ``` Create an Intune Remediation that detects unauthorized local administrator accounts on our Windows endpoints. Build a PowerShell detection script that enumerates the local Administrators group, excludes the accounts in our allowlist ({allowed_accounts}), and reports any others. Deploy it in detect-only mode to the {pilot_group} device group on a daily schedule, then show me the results. ``` ## Manual steps (admin center) 1. Sign in to the [Microsoft Intune admin center](https://intune.microsoft.com) with an Intune Administrator account. 2. In the left navigation, go to **Devices** > **Manage devices** > **Scripts and remediations**. 3. Select the **Remediations** tab, then click **Create**. 4. On the **Basics** page, enter:Click **Next**. - **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 5. On the **Settings** page:Click **Next**. - **Detection script file:** upload your detection script (see the PowerShell section for the logic it must follow). - **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`. 6. On the **Scope tags** page, apply any scope tags required by your RBAC model, then click **Next**. 7. On the **Assignments** page, assign the remediation to a pilot device group first (for example, `Intune-Pilot-Devices`). Set the schedule to **Daily** at a low-impact hour. 8. Review the summary and click **Create**. ### Review detection results 1. After devices check in, go back to **Devices** > **Scripts and remediations** and open **Detect Unauthorized Local Administrators**. 2. Open the **Device status** tab to see per-device results. 3. Add the column **Pre-remediation detection output** to see the list of unexpected accounts reported by each device. 4. Click **Export** to download a CSV of non-compliant devices for follow-up. ## Microsoft Graph API 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 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": "",  "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": ""      },      "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=managedDevice ``` The `preRemediationDetectionScriptOutput` property on each run state contains the list of unexpected account names reported by that device. ## PowerShell The detection script should follow this logic: - Resolve the localized name of the built-in Administrators group through its well-known SID `S-1-5-32-544` (required for non-English Windows installs). - Enumerate the group with `Get-LocalGroupMember`. - Filter to `PrincipalSource -eq 'Local'` so domain and Entra ID accounts are ignored. - Compare the remaining accounts against an `$allowedAccounts` allowlist. - Exit `1` with a descriptive message when unexpected accounts exist (Intune reports *Issue detected*), or `0` when the device is clean. ### Before deploying 1. Set the `$allowedAccounts` array to your approved accounts, typically the renamed built-in Administrator, your LAPS managed account, and any sanctioned break-glass accounts. 2. Test locally by running the script in an elevated PowerShell session on a reference device and checking the exit code: ``` powershell.exe -ExecutionPolicy Bypass -File .\Detect-LocalAdmins.ps1; echo $LASTEXITCODE ``` 3. Confirm the exit code matches expectations (0 = clean, 1 = flagged). 4. Upload the validated script to the Intune remediation package you created. ### Extending to auto-remediation (phase 2) After 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 it to a small ring first and review the *Post-remediation detection output* column before you broaden the scope.