Forum Discussion

charlie4872's avatar
charlie4872
Brass Contributor
Apr 27, 2022

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....
  • LainRobertson's avatar
    Apr 28, 2022

    charlie4872 

     

    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

Resources