Effective Strategies for Detecting and Removing Users from the Administrators Group
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