function Kill-ProcessByPort { param([int]$Port) try { $processId = (Get-NetTCPConnection -LocalPort $Port -ErrorAction Stop).OwningProcess; Stop-Process -Id $processId -Force; Write-Output "Process using port $Port has been terminated." } catch { Write-Output "No process found using port $Port" } }Usage:
Kill-ProcessByPort -Port 5008