As I noted in my book, PowerShell for Systems Engineers, I often feel like PowerShell is underutilized for network monitoring and diagnostics. It offers a host of tools that are effective for doing a variety of things. Below are 21 one-liners that will enable you to realize PowerShell’s networking potential. If you are looking for a deep dive in PowerShell, be sure to check out my course on Udemy. You can also see my video on YouTube where I actually execute all of these commands.
--Retrieve the IP configuration of all network adapters.
Get-NetIPAddress
--Retrieve a list of all network interfaces and their associated IP addresses.
Get-NetIPAddress | Select-Object -Property InterfaceAlias, IPAddress | Format-Table
--Send a single ICMP echo request to a remote host.
Test-Connection -ComputerName "microsoft.com"
--Send a single ICMP echo request to a remote host while specifying counts
Test-Connection -ComputerName "microsoft.com" -Count 1
--Test if a specific port is open on a remote server.
Test-NetConnection -ComputerName "microsoft.com" -Port 80
--List all network adapters on the computer.
Get-NetAdapter
--Find MAC Address: Retrieve the MAC address of a network adapter.
Get-NetAdapter | Select-Object -Property Name, MacAddress
--Display detailed information about a specific network adapter.
Get-NetAdapter | Where-Object { $_.Name -eq "Ethernet" } | Format-Table -AutoSize
--Show network statistics for a specific network adapter.
Get-NetAdapterStatistics -Name "Ethernet"
--List open ports on your local computer.
Get-NetTCPConnection -State Listen
--Find Active Network Connections: List active network connections with local and remote endpoints.
Get-NetTCPConnection | Where-Object { $_.State -eq 'Established' }
--Show the DNS server addresses of the current network connection.
Get-DnsClientServerAddress
--Change DNS Servers: Set DNS server addresses for a network adapter.
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses ("8.8.8.8", "8.8.4.4")
--Clear the DNS resolver cache to refresh DNS information.
Clear-DnsClientCache
--Perform a trace route to a destination IP address.
Test-NetConnection -ComputerName "microsoft.com" -TraceRoute
--Retrieve the IP address associated with a domain name (A record).
Resolve-DnsName -Name "powershellengineer.com" -Type A
--Get the mail server records (MX records) for a domain.
Resolve-DnsName -Name "powershellengineer.com" -Type MX
--Resolve an IP address to its associated domain name.
[System.Net.Dns]::GetHostEntry("192.168.1.1").HostName
--Retrieve the Address Resolution Protocol (ARP) table.
Get-NetNeighbor -AddressFamily IPv4
--List all Allow firewall rules.
Get-NetFirewallRule | Where-Object { $_.Action -eq "Allow" }
--List all Allow firewall rules.
Get-NetFirewallRule | Where-Object { $_.Action -eq "Block" }
you list "--List all Allow firewall rules." twice. I think the second one should be 'block' which matches the command.
Also, thanks for the cheat sheet.