r/PowerShell • u/netmc • Feb 27 '26
Remove Users from Local Administrators Group (ADSI/.Net)
I'm aware that the PowerShell functions for working with local groups in PS 5.1 are broken. I've had some luck working around this utilizing ADSI and .Net methods. For reading the accounts, I use ADSI as it doesn't need to download the entirety of the AD objects to return a list of accounts. This part all works fine. What I'm running into issue with is removing domain accounts from the local administrators group.
Add-Type -AssemblyName System.DirectoryServices.AccountManagement -ErrorAction Stop
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $env:COMPUTERNAME
$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName
$sidtype = [System.DirectoryServices.AccountManagement.IdentityType]::Sid
$ADSIComputer = [ADSI]("WinNT://$env:COMPUTERNAME,computer")This part all works fine. Because of unresolvable SIDs and AzureAD SIDs not working well with ADSI methods, I try and use the .Net methods for removing accounts from the group.
$AdminGroup=[System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context,'Administrators')
$UserSID='S-1-5-21-XXXXXXXXXX-XXXXXXXX-XXXXXXXXX-1137'
[void]$admingroup.members.Remove($context,$sidtype,$userSID)
$admingroup.save()This works for local accounts, orphaned accounts and AzureAD accounts, but when it comes to active domain accounts the .Remove() method errors with: "No principal matching the specified parameters was found."
I tried switching to use SAM account name instead, but still receive the same error.
[void]$admingroup.members.Remove($context,$idtype,"DOMAIN\User")
$admingroup.save()I've got something wrong, but I'm not exactly sure what. Has anyone run into this before and do you have a workaround or alternate method?
8
u/chaosphere_mk Feb 28 '26
Just install powershell 7 and use the normal commands? I wasnt aware 5.1 had issues with this. Ive never run into a problem, but I've been using powershell 7 for so long that maybe I just missed it.
2
u/JwCS8pjrh3QBWfL Feb 27 '26
I see you noted AzureAD accounts, are these devices in Intune? If so, you can use a policy at "Endpoint Security > Account protection > Create Policy > Local user group membership" to modify the Administrators group rather than powershell. There are some guides online on how to calculate the SIDs for Entra groups that should be in there (like the Entra Device Administrator role) so that you don't blow those out.
1
u/dodexahedron Feb 28 '26
How about using GP and just clearing all members of the group?
1
u/krzydoug Mar 01 '26
First thing that stands out (and may not matter to you) is this only works for systems in English (or otherwise local admins group is "Administrators"). This is what I came up with when dealing with similar issues as you. This will remove the group members via their ADSIPath. Give it a try and let me know please!
1
u/[deleted] Mar 08 '26
[removed] — view removed comment