top of page
Search

Effective Strategies for Detecting and Removing Users from the Administrators Group

Aug 24
2 min read

Updated: 5 days ago

šŸ” Intune Blog: Detecting & Removing Individual User Accounts from Administrators Group


šŸ“Œ Why This Matters

Maintaining a secure environment means ensuring that only authorized accounts have administrative privileges. Individual user accounts in the Administrators groupĀ can pose a serious risk — they bypass standard security controls and increase the attack surface.

With Intune detection and remediation scripts, IT admins can automatically identify and remove such accounts, ensuring compliance across endpoints.

šŸ•µļø Detection Script

The detection script checks for individual user accounts in the Administrators groupĀ (excluding the built-in Administrator). If found, it logs the details and marks the device as Non-Compliant.

powershell

# Create log folder if needed
$LogFolderĀ = "C:\Logs"
$LogFileĀ = "$LogFolder\Detection.txt"

ifĀ (!(Test-PathĀ $LogFolder)) {
    New-ItemĀ -PathĀ $LogFolderĀ -ItemTypeĀ Directory -ForceĀ | Out-Null
}

$DateTimeĀ = Get-DateĀ -FormatĀ "yyyy-MM-dd HH:mm:ss"

tryĀ {
    $AdminsĀ = Get-LocalGroupMemberĀ -GroupĀ "Administrators"Ā -ErrorActionĀ Stop

    $IndividualUsersĀ = $AdminsĀ | Where-ObjectĀ {
        $_.ObjectClass -eqĀ "User"Ā -and
        $_.Name -notmatchĀ "\\Administrator$"
    }

    ifĀ ($IndividualUsers) {
        $UserListĀ = ($IndividualUsers.Name -joinĀ ", ")
        @"
[$DateTime] Non-Compliant
Individual user account(s) found in Administrators group:
$UserList
"@Ā | Out-FileĀ -FilePathĀ $LogFileĀ -EncodingĀ UTF8
        Write-OutputĀ "Non-Compliant: $UserList"
        exitĀ 1
    }
    elseĀ {
        @"
[$DateTime] Compliant
No individual user accounts found in Administrators group.
"@Ā | Out-FileĀ -FilePathĀ $LogFileĀ -EncodingĀ UTF8
        Write-OutputĀ "Compliant"
        exitĀ 0
    }
}
catchĀ {
    "[$DateTime] ERROR: $_"Ā | Out-FileĀ -FilePathĀ $LogFileĀ -EncodingĀ UTF8
    exitĀ 1
}

šŸ› ļø Remediation Script

The remediation script removes any detected individual user accounts from the Administrators groupĀ and logs the action.

powershell

# Create log folder if needed
$LogFolderĀ = "C:\Logs"
$LogFileĀ = "$LogFolder\Remediation.txt"

ifĀ (!(Test-PathĀ $LogFolder)) {
    New-ItemĀ -PathĀ $LogFolderĀ -ItemTypeĀ Directory -ForceĀ | Out-Null
}

$DateTimeĀ = Get-DateĀ -FormatĀ "yyyy-MM-dd HH:mm:ss"

tryĀ {
    $AdminsĀ = Get-LocalGroupMemberĀ -GroupĀ "Administrators"Ā -ErrorActionĀ Stop

    $IndividualUsersĀ = $AdminsĀ | Where-ObjectĀ {
        $_.ObjectClass -eqĀ "User"Ā -and
        $_.Name -notmatchĀ "\\Administrator$"
    }

    ifĀ ($IndividualUsers) {
        foreachĀ ($UserĀ inĀ $IndividualUsers) {
            tryĀ {
                Remove-LocalGroupMemberĀ -GroupĀ "Administrators"Ā -MemberĀ $User.Name -ErrorActionĀ Stop
                Add-ContentĀ -PathĀ $LogFileĀ -ValueĀ "[$DateTime] Removed user: $($User.Name)"
            }
            catchĀ {
                Add-ContentĀ -PathĀ $LogFileĀ -ValueĀ "[$DateTime] Failed removing user: $($User.Name) - $_"
            }
        }
        Write-OutputĀ "Remediation completed."
    }
    elseĀ {
        Add-ContentĀ -PathĀ $LogFileĀ -ValueĀ "[$DateTime] No individual user accounts found."
    }
    exitĀ 0
}
catchĀ {
    Add-ContentĀ -PathĀ $LogFileĀ -ValueĀ "[$DateTime] ERROR: $_"
    exitĀ 1
}

āš ļø Important Note

These scripts are provided as a baseline. šŸ‘‰ Always modify the script as per your organizational requirements — for example, to exclude specific service accounts or tailor logging paths.



Ā 
Ā 
Ā 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Disclaimer: The above content is created at Tek-Doyen's sole discretion. 

bottom of page