# Kill processes occupying specified ports param( [int[]]$Ports = @(5173, 5174,5057) ) foreach ($port in $Ports) { $connections = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue if (-not $connections) { Write-Host "Port $port : no process listening" -ForegroundColor Gray continue } $procIds = $connections | Select-Object -ExpandProperty OwningProcess -Unique foreach ($procId in $procIds) { $proc = Get-Process -Id $procId -ErrorAction SilentlyContinue if ($proc) { Write-Host "Port $port : killing $($proc.ProcessName) (PID $procId)" -ForegroundColor Yellow Stop-Process -Id $procId -Force } } } Write-Host "Done." -ForegroundColor Green