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
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:
$members = Get-LocalGroupMember -Name $Admingroup
Then, reference the member via:
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:
"pl\prg_test_1"{$Administrators += $member.Name}
Then, if you want to stick with PowerShell, remove the member as such:
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
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