Forum Discussion
Remove list of users from Local Administrators group on list of 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
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
Thanks!