---
# System prepended metadata

title: 列出程式使用port
tags: [script, powershell, windows]

---

# 列出程式使用port
###### tags: `script`, `powershell`, `windows`


`for /f "tokens=2 delims=," %i in ('tasklist /fi "imagename eq anydesk.exe" /fo csv') do ( netstat -ano | findstr %~i)`

``` batch
@echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims=," %%i in ('tasklist /fi "imagename eq %1" /fo csv') do (
    netstat -ano | findstr %%~i
)
```

`netstat -ano`


``` powershell

Get-NetTCPConnection | Where-Object {$_.State -eq "Established"} | Select-Object LocalAddress, LocalPort, RemoteAddress, RemotePort, @{Name="Process";Expression={(Get-Process -Id $_.OwningProcess).ProcessName}} | Format-Table -AutoSize

```