How to find and remove security group from local admin group with Powershell

Cody 321 Reputation points
2024-09-23T20:43:06.51+00:00

How would I write this in PS where I'm looking for or checking for "Company\Domain Admins" security group from local Administrators group?

Get-LocalGroupMember -Group "Administrators" (Gives me a list of groups that are in the Local Administrators group)

If exist ""Company\Domain Admins"

Then

Remove-LocalGroupMember -Group "Administrators" -Member "Company\Domain Admins"

What would be the correct syntax?

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,517 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,489 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 36,661 Reputation points Microsoft Vendor
    2024-09-24T00:51:49.5966667+00:00

    Hi Cody,

    Thanks for your post. You can try the following command

    Get-LocalGroupMember -Group "Administrators" | Where-Object {$_.name -eq "Company\Domain Admins" } | Remove-LocalGroupMember -Group "Administrators"
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Cody 321 Reputation points
    2024-09-24T15:45:53.9366667+00:00

    Thank you, Ian Xue. That's what I needed.

    0 comments No comments

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.