question

LoganM-6822 avatar image
0 Votes"
LoganM-6822 asked DineshaGovindaiah-9755 commented

Remove local group member from multiple groups on multiple servers

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered DineshaGovindaiah-9755 commented

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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot @IanXue-MSFT, it worked

0 Votes 0 ·

Now I ran into the below error on few servers, should this be because of missing AD module

The term 'Remove-LocalGroupMember' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Remove-LocalGroupMember:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : XYZ

0 Votes 0 ·

Hi @IanXue-MSFT

Thank you for the script, its working for few servers and for few we are getting error as below
And it's possible to export the output for each computer in the CSV?

The term 'Remove-LocalGroupMember' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Remove-LocalGroupMember:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException



0 Votes 0 ·