Hi @mara2021 ,
if you want to get the members of a local group on your computer please try this script:
$groupName = "Testgroup"
$filePath = "C:\Junk\"
$userlist = $((Get-LocalGroupMember -Group "$groupName").Name).Replace("$env:COMPUTERNAME\", "")
$userlist | ForEach-Object {
if (Test-Path $filePath\$_.txt -PathType Leaf) {
Write-Output "$_ user file exists"
}
else {
Write-Output "$_ user file does not exists .... removing user from local group"
Remove-LocalGroupMember -Group "$groupName" -Member $_ -WhatIf
}
}
The -WhatIf
parameter in line 10 will show what will happen, but will not remove the user from group. If everything is like expected just remove the -WhatIf
parameter in line 10 and the user will be removed from the group
----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten