cheat-sheets

Windows Networking

Inspecting and configuring networking on Windows from the command line: classic tools (ipconfig, netsh, route, netstat) and the newer PowerShell cmdlets (Get-NetIPConfiguration, Test-NetConnection, Resolve-DnsName, New-NetFirewallRule). Run elevated for changes. Commands run in PowerShell (PowerShell Cheat-Sheet) unless noted. See also Windows DNS Server, Windows Troubleshooting, WSL Cheat-Sheet and Linux equivalents in Linux Basics.

Documentation: NetTCPIP module, NetSecurity module

IP Configuration #

COMMANDDESCRIPTION
ipconfigIP, mask and gateway of each adapter
ipconfig /allFull detail: MAC, DHCP server, DNS servers, lease times
ipconfig /release / ipconfig /renewDrop and request a DHCP lease
ipconfig /flushdnsClear the DNS resolver cache
ipconfig /displaydnsShow the DNS resolver cache
ipconfig /registerdnsRe-register this host’s records in DNS
Get-NetIPConfigurationSummary per interface (IP, gateway, DNS)
Get-NetIPAddress -AddressFamily IPv4All IPv4 addresses
Get-NetAdapterAdapters with status, speed and MAC
Get-NetAdapter | Where Status -eq UpActive adapters
Get-NetIPInterfaceInterface metrics and DHCP state
New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress <IP> -PrefixLength 24 -DefaultGateway <GW>Set a static IP
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses <DNS1>,<DNS2>Set DNS servers
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddressesBack to DHCP-provided DNS
Set-NetIPInterface -InterfaceAlias "Ethernet" -Dhcp EnabledSwitch to DHCP (remove the static address and gateway first)
Rename-NetAdapter -Name "Ethernet" -NewName "LAN"Rename an adapter
Disable-NetAdapter -Name "Wi-Fi" -Confirm:$false / Enable-NetAdapter -Name "Wi-Fi"Disable or enable an adapter
Get-NetConnectionProfileNetwork category: Public, Private or DomainAuthenticated
Set-NetConnectionProfile -InterfaceAlias "Ethernet" -NetworkCategory PrivateChange the category (affects firewall profile)

Test Connectivity #

COMMANDDESCRIPTION
ping <HOST>ICMP echo (-t continuous, -n 10 count, -l 1400 size)
Test-Connection <HOST> -Count 4PowerShell ping, returns objects
Test-NetConnection <HOST>Ping plus resolved address and interface
Test-NetConnection <HOST> -Port 443TCP port check (replaces telnet)
Test-NetConnection <HOST> -Port 3389 -InformationLevel QuietJust True or False
Test-NetConnection <HOST> -TraceRouteTraceroute
Test-NetConnection -ComputerName <HOST> -CommonTCPPort SMBCheck a common service (HTTP, RDP, WINRM, SMB)
1..1024 | ForEach-Object { if ((Test-NetConnection <HOST> -Port $_ -WarningAction SilentlyContinue).TcpTestSucceeded) { $_ } }Slow port scan (use NMap for real scans)
tracert <HOST>Classic traceroute (-d skips name lookups)
pathping <HOST>Traceroute with per-hop loss statistics (takes about 5 minutes)
curl.exe -I https://<HOST>HTTP header check with the bundled curl
Test-WSMan <HOST>Check WinRM (PowerShell Remote Access)

Add -WarningAction SilentlyContinue to Test-NetConnection to hide warnings when ICMP is blocked.

DNS #

COMMANDDESCRIPTION
Resolve-DnsName <HOST>Lookup (A and AAAA)
Resolve-DnsName <HOST> -Type MXRecord type (A, AAAA, CNAME, TXT, NS, SRV, PTR)
Resolve-DnsName <HOST> -Server <DNS-IP>Ask a specific DNS server
Resolve-DnsName <HOST> -DnsOnlySkip hosts file, LLMNR and NetBIOS
Resolve-DnsName -Type SRV _ldap._tcp.dc._msdcs.<DOMAIN>Find domain controllers via DNS (Active Directory)
Resolve-DnsName <IP>Reverse lookup (PTR)
nslookup <HOST> <DNS-IP>Classic lookup (set type=MX interactive)
Get-DnsClientServerAddressDNS servers per interface
Get-DnsClientCacheResolver cache as objects
Clear-DnsClientCacheFlush the cache (same as ipconfig /flushdns)
Get-DnsClientNrptPolicyName Resolution Policy Table (DirectAccess, VPN)
Add-DnsClientNrptRule -Namespace ".lab.local" -NameServers <DNS-IP>Send one domain to a specific DNS server
Get-Content C:\Windows\System32\drivers\etc\hostsHosts file (edit as admin)

A short checklist for “can’t resolve a name”: ipconfig /all (which DNS server), Resolve-DnsName <HOST> -Server <DNS-IP>, Clear-DnsClientCache, hosts file, VPN/NRPT rules. See DNS Record Types for record types.

Ports and Connections #

COMMANDDESCRIPTION
netstat -anoAll connections and listeners with owning PID
netstat -ano | findstr :443Filter by port
netstat -abnoInclude the executable name (needs admin)
Get-NetTCPConnection -State ListenListening TCP ports as objects
Get-NetTCPConnection -LocalPort 443Connections on a port
Get-NetTCPConnection -State Established | Group-Object RemoteAddress | Sort Count -DescTop remote peers
Get-Process -Id <PID>Which process owns a PID
Get-NetUDPEndpointUDP listeners
(Get-NetTCPConnection -LocalPort 8080).OwningProcess | ForEach-Object { Get-Process -Id $_ }Process behind a port

Routing #

COMMANDDESCRIPTION
route printRouting table (IPv4 and IPv6)
Get-NetRouteRoutes as objects (-AddressFamily IPv4)
Get-NetRoute -DestinationPrefix 0.0.0.0/0Default gateway route
route add 10.10.0.0 mask 255.255.0.0 <GW>Add a temporary route (lost on reboot)
route -p add 10.10.0.0 mask 255.255.0.0 <GW>Add a persistent route
New-NetRoute -DestinationPrefix 10.10.0.0/16 -InterfaceAlias "Ethernet" -NextHop <GW>Add a route in PowerShell (persistent by default)
Remove-NetRoute -DestinationPrefix 10.10.0.0/16 -Confirm:$falseRemove a route
route delete 10.10.0.0Remove a route (classic)
Find-NetRoute -RemoteIPAddress <IP>Which route and source address would be used

ARP and Neighbours #

COMMANDDESCRIPTION
arp -aARP table
Get-NetNeighborARP/ND cache as objects (-State Reachable)
Remove-NetNeighbor -IPAddress <IP> -Confirm:$falseDrop one entry
netsh interface ip delete arpcacheClear the ARP cache
Get-NetAdapter | Select Name,MacAddressLocal MAC addresses

Firewall (Windows Defender Firewall) #

COMMANDDESCRIPTION
Get-NetFirewallProfileState of the Domain, Private and Public profiles
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled TrueTurn the firewall on (avoid disabling it)
Get-NetFirewallRule -Enabled True -Direction InboundActive inbound rules
Get-NetFirewallRule -DisplayName "*Remote Desktop*"Search rules by name
Get-NetFirewallRule -DisplayName "Allow HTTPS" | Get-NetFirewallPortFilterPorts in a rule
New-NetFirewallRule -DisplayName "Allow HTTPS In" -Direction Inbound -Protocol TCP -LocalPort 443 -Action AllowOpen an inbound port
New-NetFirewallRule -DisplayName "Allow SSH from LAN" -Direction Inbound -Protocol TCP -LocalPort 22 -RemoteAddress 192.168.1.0/24 -Action Allow -Profile PrivateRestrict by source and profile
New-NetFirewallRule -DisplayName "Allow MyApp" -Direction Inbound -Program "C:\Apps\myapp.exe" -Action AllowAllow a program
New-NetFirewallRule -DisplayName "Block Outbound 25" -Direction Outbound -Protocol TCP -RemotePort 25 -Action BlockBlock outbound traffic
Set-NetFirewallRule -DisplayName "Allow HTTPS In" -RemoteAddress 10.0.0.0/8Edit a rule
Disable-NetFirewallRule -DisplayName "<NAME>" / Enable-NetFirewallRule -DisplayName "<NAME>"Toggle a rule
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"Enable a built-in group
Remove-NetFirewallRule -DisplayName "<NAME>"Delete a rule
Get-NetFirewallRule | Where Direction -eq Inbound | Where Action -eq BlockBlock rules
netsh advfirewall show allprofilesClassic profile summary
netsh advfirewall export C:\fw.wfw / netsh advfirewall import C:\fw.wfwBack up and restore firewall policy
netsh advfirewall resetReset to defaults (removes custom rules)
netsh advfirewall set allprofiles logging droppedconnections enableLog dropped packets (default log %systemroot%\system32\LogFiles\Firewall\pfirewall.log)

Rule priority: explicit Block rules override Allow rules. The active profile follows the network category (see Get-NetConnectionProfile). Domain GPOs (Group Policy) can override local rules. For ufw on Linux see UFW (uncomplicated firewall).

netsh #

COMMANDDESCRIPTION
netsh interface ip show configIP settings per interface
netsh interface ip set address "Ethernet" static <IP> <MASK> <GW>Set static IP
netsh interface ip set address "Ethernet" dhcpUse DHCP
netsh interface ip set dns "Ethernet" static <DNS>Set DNS
netsh interface portproxy add v4tov4 listenport=<PORT> connectport=<PORT> connectaddress=<IP>Port forward (see WSL Cheat-Sheet)
netsh interface portproxy show allList port proxies
netsh winsock resetReset Winsock (reboot after)
netsh int ip resetReset the TCP/IP stack (reboot after)
netsh wlan show profilesSaved Wi-Fi networks
netsh wlan show profile name="<SSID>" key=clearShow a saved Wi-Fi key (admin; it prints a secret)
netsh wlan show interfacesCurrent Wi-Fi connection and signal
netsh trace start capture=yes tracefile=C:\trace.etl / netsh trace stopPacket capture without extra software (convert the .etl for Wireshark with the etl2pcapng tool)
netsh http show urlaclHTTP URL reservations

Shares and Sessions #

COMMANDDESCRIPTION
net useMapped drives and sessions
net use Z: \\<SERVER>\<SHARE> /persistent:yesMap a drive (prompts for credentials if needed)
net use Z: /deleteRemove the mapping
New-SmbMapping -LocalPath Z: -RemotePath \\<SERVER>\<SHARE>PowerShell mapping
Get-SmbShareShares on this machine
Get-SmbConnectionOutgoing SMB connections
Get-SmbSessionClients connected to this server
Get-SmbOpenFileFiles opened over SMB (Close-SmbOpenFile to close)
net view \\<SERVER>Shares on a server

Reset Network Stack #

For when networking is broken beyond DNS (run elevated, then reboot):

powershell
netsh winsock reset
netsh int ip reset
ipconfig /release
ipconfig /renew
ipconfig /flushdns

Also try Restart-NetAdapter -Name "Ethernet" and checking for stale VPN adapters with Get-NetAdapter -IncludeHidden.

Not Verified #

Commands were written from the Microsoft documentation and not executed. Cmdlet availability differs on Windows Server Core and in PowerShell 7 (some NetTCPIP cmdlets load through Windows PowerShell compatibility).

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.