Skip to content

Instantly share code, notes, and snippets.

@jschlackman
Last active December 19, 2023 21:57
Show Gist options
  • Save jschlackman/92eab313ade44ec3d92b53291df310ad to your computer and use it in GitHub Desktop.
Save jschlackman/92eab313ade44ec3d92b53291df310ad to your computer and use it in GitHub Desktop.
# Name: Remove-ADUserAdmins.ps1
# Author: James Schlackman
# Last Modified: Dec 19 2023
# Clear individual non-local (e.g. AD) users from the local administrators group
# Notes:
# - Uses CIM to retrieve local group membership as Get-LocalGroupMember does not work without DC connectivity
# - Must be run in 64-bit PowerShell Host. The LocalAccounts module is not available in 32-bit PowerShell on a 64-bit system.
#Requires -Modules Microsoft.PowerShell.LocalAccounts
#Requires -RunAsAdministrator
$AdminUsers = (Get-CimInstance Win32_GroupUser | Where-Object {$_.PartComponent.CimClass.CimClassName -eq 'Win32_UserAccount' -and $_.GroupComponent.Name -eq 'Administrators' -and $_.PartComponent.Domain -ne $env:COMPUTERNAME}).PartComponent
$AdminUsers | Where-Object {$_} | ForEach-Object {Remove-LocalGroupMember -Group 'Administrators' -Member "$($_.Domain)\$($_.Name)"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment