Forum Discussion
charlie4872
Apr 27, 2022Brass Contributor
Remove list of users from Local Administrators group on list of computers
Hello, we are trying to remove users from the local administrators group on a bunch of computers. After that we will implement a GPO to control who is added to the local admin group on all computers....
- Apr 28, 2022
Hi.
There's a number of issues with your "Invoke-Command" statement, the most notable being this part:
where {$_.objectclass -like $users}
$_.objectClass isn't something you're ever going to measure a user account name against.
We also can't see examples of the data you're pulling from the users.txt and computers.txt files, meaning there could be issue there, too, and we wouldn't know.
Anyhow, forging ahead.
Let's say your data from computers.txt looks like this:
client01.mydomain.com client02.mydomain.com client03.mydomain.com
And your users.txt looks like this:
mydomain\user01 mydomain\user02 mydomain\user03
Then your script - in its simplest form - would look like this:
Invoke-Command -ComputerName (Get-Content -Path .\computers.txt) -ArgumentList (Get-Content -Path .\users.txt) -ScriptBlock { $args | Remove-LocalGroupMember -Group "Administrators" -ErrorAction:SilentlyContinue; }
Cheers,
Lain
Dave Patrick
Apr 27, 2022MVP
You can follow along here.
How to remove bulk user local admin right wirh Powershell - Microsoft Q&A