This is my code:
try {
$usernames = Get-LocalUser | Where-Object { $_.enabled -EQ 'True' } | Select-Object 'Name'
$usernames | ForEach-Object { Add-LocalGroupMember -Group 'Hyper-V Administrators' -Member $_.Name }
}
catch {
Write-Host "All user accounts are already part of the Hyper-V Administrators group `n" -ForegroundColor black -BackgroundColor yellow
}
when I run it, I still see errors in the console, the catch never runs. what's the problem?
-ErrorAction Stopto tell powershell you want to treat it as a terminating error:Add-LocalGroupMember -Group "Hyper-V Administrators" -Member $_.Name -ErrorAction Stopcatch