Windows PowerShell common networking commands
Windows PowerShell common networking commands (IPCONFIG, PING, TRACERT, ROUTE NSLOOKUP,NETSTAT)
Network Engineer to trouble shoot some of Windows Servers issue apart from Networking Devices. Maybe you need to check the IP address of a machine or test if its networking connection is working. Maybe you need to see if DNS is properly configured or check the latency between two hosts.
If you have been in this field long enough, you probably have a few favorite commands that you learned years ago and use on a regular basis, like IPCONFIG or PING.
There are literally hundreds of networking-related PowerShell cmdlets in Windows these days. Just try out this command on your machine:
Get-Command -Module Net* | Group Module – Give you all the list available in your Windows Servers or Windows 10
But more important than knowing every one of them, is to know the most useful cmdlets that have the potential to replace those old commands that you can’t live without.
Only PowerShell can do that you’ll find amazing new troubleshooting abilities…
IPCONFIG
Description: This command has many options, but the most common usage is just to show the IP address, subnet mask and default gateway for each network adapter in a machine.
PowerShell: Get-NetIPConfiguration or Get-NetIPAddress
Sample command lines:
- 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
Description: Checks connectivity to a specific host. Commonly used to check for liveliness, but also used to measure network latency.
PowerShell: Test-NetConnection
Sample command lines:
- Test-NetConnection www.google.co.uk
- Test-NetConnection -ComputerName www.google.co.uk -InformationLevel Detailed
- Test-NetConnection -ComputerName www.google.co.uk | Select -ExpandProperty PingReplyDetails | FT Address, Status, RoundTripTime
- 1..10 | % { Test-NetConnection -ComputerName www.microsoft.com -RemotePort 80 } | FT -AutoSize
NSLOOKUP
Description: Name server lookup. Mostly used to find the IP address for a given DNS name (or vice-versa). Has many, many options.
PowerShell: Resolve-DnsName
Sample command lines:
- Resolve-DnsName www.cisco.com
- Resolve-DnsName www.cisco.com -type SOA
- Resolve-DnsName cisco.com -Server 8.8.8.8 –Type A
ROUTE
Description: Shows the IP routes in a given system (also used to add and delete routes)
PowerShell: Get-NetRoute (also New-NetRoute and Remove-NetRoute)
Sample command lines:
- Get-NetRoute -Protocol Local -DestinationPrefix 192.168*
- Get-NetAdapter Wi-Fi | Get-NetRoute
TRACERT
Description: Trace route. Shows the IP route to a host, including all the hops between your computer and that host.
PowerShell: Test-NetConnection –TraceRoute
Sample command lines:
- Test-NetConnection www.google.co.uk –TraceRoute
- Test-NetConnection hotmail.com -TraceRoute | Select -ExpandProperty TraceRoute | % { Resolve-DnsName $_ -type PTR -ErrorAction SilentlyContinue }
NETSTAT
Description: Shows current TCP/IP network connections.
PowerShell: Get-NetTCPConnection
Sample command lines:
- 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 }
It is very interesting to know these commands in Powershell.
I have tested some range to ping using Powershesll below command sweeps all the range of 192.168.1.0/24
1
2
3
4
5
|
$a = 200
$z = 250
$a..$z | foreach {
$ip = “192.168.1.0” -replace “0$”,$_
Test-Connection -ComputerName $IP -Count 1 -Quiet}
|
Happy Labbing !