PowerShell equivalents of classic network commands
PowerShell cmdlets replacing ipconfig, ping, nslookup, route, tracert and netstat.
PowerShell equivalents of classic network command-line tools (ipconfig, ping, nslookup, route, tracert, netstat).
ipconfig
Get-NetIPConfiguration
Get-NetIPAddress | Sort InterfaceIndex | FT InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress, PrefixLength -Autosize
Get-NetIPAddress | ? AddressFamily -eq IPv4 | FT -AutoSize
Get-NetAdapter Wi-Fi | Get-NetIPAddress | FT -AutoSize
ping
Test-NetConnection -ComputerName sqlserver01 -InformationLevel Detailed
Test-NetConnection -ComputerName sqlserver01 | Select -ExpandProperty PingReplyDetails | FT Address, Status, RoundTripTime -Autosize
1..10 | % { Test-NetConnection -ComputerName sqlserver01 -RemotePort 80 } | FT -AutoSize
nslookup
Resolve-DnsName www.example.com
Resolve-DnsName example.com -Type SOA
Resolve-DnsName example.com -Server 192.0.2.53 -Type A
route
Get-NetRoute -Protocol Local -DestinationPrefix 192.0.2*
Get-NetAdapter Wi-Fi | Get-NetRoute
tracert
Test-NetConnection www.example.com -TraceRoute
Test-NetConnection sqlserver01 -TraceRoute | Select -ExpandProperty TraceRoute | % { Resolve-DnsName $_ -Type PTR -ErrorAction SilentlyContinue }
netstat
Get-NetTCPConnection | Group State, RemotePort | Sort Count | FT Count, Name -Autosize
Get-NetTCPConnection | ? State -eq Established | FT -Autosize
Get-NetTCPConnection | ? State -eq Established | ? RemoteAddress -notlike 127* | % { $_; Resolve-DnsName $_.RemoteAddress -Type PTR -ErrorAction SilentlyContinue }
Listening ports
Get-NetTCPConnection | ? {$_.State -eq "Listen"}
Get-NetTCPConnection | ? {($_.State -eq "Listen") -and ($_.RemoteAddress -eq "0.0.0.0")}
Get-NetTCPConnection | ? {($_.State -eq "Listen") -and ($_.RemoteAddress -eq "0.0.0.0")} | select * | Export-Csv net-connect.csv -NoTypeInformation
Find (and stop) the processes behind listening ports
$processes = (Get-NetTCPConnection | ? {($_.State -eq "Listen") -and ($_.RemoteAddress -eq "0.0.0.0")}).OwningProcess
foreach ($process in $processes) {Get-Process -PID $process | select ID,ProcessName}
foreach ($process in $processes) {Get-Process -PID $process | Stop-Process -Force}
Notes
- The last command forcibly stops every process listening on all interfaces; review the list first.