# Remove users from local admin group with exclusions

**URL:** <https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521>\
**Category:** PowerShell Help\
**Created:** [February 1, 2024, 6:45pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521 "2024-02-01T18:45:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 1, 2024, 6:45pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/1 "2024-02-01T18:45:18Z")

</div>

Hi all,

I am looking for help  
I am trying to remove users from local admin group with exclusions  
the script working almost correct when i run it on my machine, but failing when it pushed by SCCM  
This is the script

```auto
function Get-GroupBySid () {
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Alias("Name")]
[string]$ComputerName,
[string]$GroupNameSid = "S-1-5-32-544"
)
$objSID = New-Object System.Security.Principal.SecurityIdentifier($GroupNameSid)
$objgroup = $objSID.Translate( [System.Security.Principal.NTAccount])
$objgroupname = ($objgroup.Value).Split("\")[1]
return ,$objgroupname
}
$Admingroup = Get-GroupBySid -ComputerName $ENV:COMPUTERNAME #localhost
$members = net localgroup $Admingroup #| where {$_ -and $_ -notmatch "command completed successfully"} | select -skip 8
$Adminaccounts = $null
$Adminaccounts = @()

foreach ($member in $members)
{

switch ($member)
{

"pl\prg_test_1"{}
"pl\prg_test_1"{}
"pl\pprg_test_2"{}
"pl\pprg_test_2"{}
"pl\strd*" {}

default {$Adminaccounts += $member}
}
}

foreach ($Adminaccount in $Adminaccounts)
{
net localgroup Administrators $Adminaccount /delete
}

```

if i run script i will get couple errors but all account will be removed and also this part not working “pl\strd\*” {}

Please, help  
Thanks

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [February 1, 2024, 7:36pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/2 "2024-02-01T19:36:34Z")

</div>

> [@brad-sam](#):
>
> but all account will be removed

That is likely because you are always taking the default action in your Switch statement. I suspect the errors are coming from the entries in $members that are not actual members since you are getting additional items from your net localgroup command.

You might try this instead:

```auto
$members = Get-LocalGroupMember -Name $Admingroup

```

Then, reference the member via:

```auto
Switch ($member.Name)

```

I would get rid of default if all you want to do is remove the members in your Switch statement and just add them to your array:

```auto
"pl\prg_test_1"{$Administrators += $member.Name}

```

Then, if you want to stick with PowerShell, remove the member as such:

```auto
Remove-LocalGroupMember -Group $AdminGroup -Member $AdminAccount

```

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 2, 2024, 3:13pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/3 "2024-02-02T15:13:57Z")

</div>

Thank you [tonys] for your approach  
I try to avoid Get-LocalGroupMember command because i received error:  
The term ‘Get-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.  
Can somebody explain why this part not working: “pl\strd\*” {}  
If any accounts start with strd - i dont want them delete, but if i run script they will be deleted

Thanks.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [February 2, 2024, 5:38pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/4 "2024-02-02T17:38:51Z")

</div>

What version of PowerShell are you using? There is also this:

The `Get-LocalGroupMember` cmdlet gets members from a local group.

Note

The Microsoft.PowerShell.LocalAccounts module is not available in 32-bit PowerShell on a 64-bit system.

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 2, 2024, 6:40pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/5 "2024-02-02T18:40:42Z")

</div>

This is the thing, I have win 7 and windows 10 machines, so PS version will be different  
but can you explain why this part not working “pl\strd\*” {}

Thanks.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [February 3, 2024, 5:41am UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/6 "2024-02-03T05:41:07Z")

</div>

> [@brad-sam](#):
>
> This is the thing, I have win 7 and windows 10 machines, so PS version will be different  
> but can you explain why this part not working “pl\strd\*” {}

Is the asterisk in the string `pl\strd*` intended to be used as a wildcard? if so, you need to include the `-Wildcard` switch on the switch statement. See [about Switch - PowerShell | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.4)

Example:

```powershell
$array = 'array1' , 'skipme', 'skipme', 'silly' , 'lol', 'whocares'

$array | ForEach-Object {
    switch -Wildcard ($_) {
        arr* { 'hello' }
        skip* { 'Skipped' }
        default {}
    }
}

```

outputs:

```output
hello
Skipped
Skipped

```

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 6, 2024, 5:44pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/7 "2024-02-06T17:44:17Z")

</div>

Thank you,  
this is what i was looking 🙂  
This small change make all work -Wildcard

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 7, 2024, 5:44pm UTC](https://forums.powershell.org/t/remove-users-from-local-admin-group-with-exclusions/23521/8 "2024-03-07T17:44:37Z")

</div>

Closed on March 7, 2024, 5:44pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
