Remove local group member from multiple groups on multiple servers

Loganayagan Uthandi Mahadevan 21 Reputation points
2021-05-20T09:39:53.717+00:00

I am able to remove Specific group members from a server through the below command, I am planning to remove Multiple members from multiple groups on multiple serrvers.

I wanna provide ComputerName, Group, and Member as input in CSV or Text files

Invoke-Command -ComputerName win2k8R2, win2k12R2 -ScriptBlock{
Remove-LocalGroupMember -Group "Administrators" -Member "xyz"
}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,602 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 38,936 Reputation points Microsoft Vendor
    2021-05-20T10:22:27.47+00:00

    Hi,

    Suppose the CSV file has columns with the headers "ComputerName", "Group" and "Member".

    ComputerName,Group,Member
    win2k8R2,administrators,abc
    win2k12R2,Administrators,xyz

    You can try something like below

    $file = 'C:\temp\members.csv'  
    Import-Csv -Path $file | ForEach-Object {  
        Invoke-Command -ComputerName $_.ComputerName -ScriptBlock{  
            Remove-LocalGroupMember -Group $using:_.Group -Member $using:_.Member  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.