$ports = @(4000, 3000, 3001) $killed = @() Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue | Where-Object { $ports -contains $_.LocalPort } | ForEach-Object { $pid = $_.OwningProcess if ($killed -notcontains $pid) { Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue $killed += $pid } }