In the ever-evolving landscape of information technology, PowerShell stands out as a fundamental tool for network administrators and IT professionals. Its powerful cmdlets (command-lets) enable a wide range of tasks necessary for managing and troubleshooting network systems efficiently. Whether you’re a beginner eager to understand the basics or an experienced professional looking to sharpen your skills, this guide provides a comprehensive look into PowerShell’s role in network operations.
Table of Contents
Introduction to PowerShell Network Cmdlets
PowerShell cmdlets are specialized .NET classes implementing a particular operation. These cmdlets form the backbone of scripting tasks in PowerShell, making it an indispensable tool for network management. From basic file operations to complex tasks like network configuration, PowerShell cmdlets offer a robust solution for IT administrators.
Testing Network Connections
Test-Connection
This cmdlet is an advanced version of the ‘ping’ command, allowing customization such as the number of ping requests (-Count) and buffer size. For example, to check connectivity to yh.do four times, you would use:
Test-Connection yh.do -Count 4
For more detailed diagnostic data, Test-NetConnection can be used. It checks not only reachability but also port status and network latency. To test if port 80 is open on yh.do, use:
Test-NetConnection yh.do -Port 80
Test-NetConnection yh.do -P 80 -InformationLevel Detailed
Test-Connection goes beyond simple ping tests to include detailed network diagnostics. It can emulate the behavior of the ping tool while offering more control over the ICMP packet characteristics. For standard ping with 128 bytes buffer size, you would use:
Test-Connection yh.do -BufferSize 128 -Count 5
For Ping using quiet mode, which returns Boolean (True/False) instead of detailed output:
Test-Connection yh.do -Quiet
To check network connectivity including traceroute:
Test-NetConnection yh.do -TraceRoute
Managing IP Addresses
1. Get-NetIPAddress Retrieve IP address configurations with Get-NetIPAddress. Filtering results for IPv4 addresses is done by:
Get-NetIPAddress -AddressFamily IPv4
2. New-NetIPAddress and Remove-NetIPAddress Adding or removing an IP address is straightforward with these cmdlets. To assign a new static IP, and then remove it, you would use:
New-NetIPAddress -IPAddress 192.168.1.2 -PrefixLength 24 -DefaultGateway 192.168.1.1
Add a secondary IP address to an interface
New-NetIPAddress -IPAddress 192.168.50.100 -PrefixLength 24 -InterfaceAlias "Ethernet"
Remove a specific IP address from an interface
Remove-NetIPAddress -IPAddress 192.168.50.100 -Confirm:$false -InterfaceAlias "Ethernet"
3. Get-NetAdapter cmdlet is an essential tool in PowerShell for querying network adapters on a Windows system. It allows IT professionals to retrieve detailed information about network interfaces, such as interface description, status, speed, and other pertinent details. This cmdlet is useful for both system administration and troubleshooting network issues.
This command retrieves and displays a list of all network adapters on the system.
Get-NetAdapter
You can specify the name of the network adapter to get details about a particular adapter.
Get-NetAdapter -Name "Ethernet"
Filter network adapters based on status, such as “Up”, “Down”, or “Disconnected”.
Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
To filter and show only physical network adapters (excluding virtual or software-based adapters).
Get-NetAdapter -Physical
Includes adapters that are not visible in the Network Connections folder.
Get-NetAdapter -IncludeHidden
If you’re only interested in specific details like the MAC address, speed, and interface description:
Get-NetAdapter | Select-Object Name, Status, MacAddress, LinkSpeed, InterfaceDescription
Resolving Domain Names
Resolve-DnsName This cmdlet helps resolve domain names to IP addresses and vice versa. For instance, to find the IP address for yh.do:
Resolve-DnsName -Name yh.do
To query for a specific type of DNS record, such as an A (address) record or an MX (mail exchange) record:
Resolve-DnsName -Name yh.do -Type A
Resolve-DnsName -Name yh.do -Type MX
Understanding Routing and Connections
1. Get-NetRoute
The Get-NetRoute cmdlet in PowerShell is used to retrieve IP routing table entries from both IPv4 and IPv6 routing tables on a specific computer. This can be used for viewing and analyzing the network routing configurations and understanding how packets are routed within networks or how they reach their destinations.
To get all routes:
Get-NetRoute
To get routes for a particular IP address family, such as IPv4:
Get-NetRoute -AddressFamily IPv4
To filter routes by their destination prefix:
Get-NetRoute -DestinationPrefix '192.168.1.0/24'
To display routes for a specific network interface by its alias:
Get-NetRoute -InterfaceAlias 'Ethernet'
2. Get-NetTCPConnection and Get-NetUDPEndpoint View active TCP and UDP connections using these cmdlets. For example, to see details about TCP connections:
Get-NetTCPConnection
Filter TCP connections by local port number
Get-NetTCPConnection -LocalPort 80
To view connections in a specific state (e.g., Established):
Get-NetTCPConnection | Where-Object { $_.State -eq "Established" }
To see all current UDP endpoints:
Get-NetUDPEndpoint
Interacting with Web and REST Services
1. Invoke-WebRequest and Invoke-RestMethod These cmdlets are essential for interacting with HTTP services and RESTful APIs. Invoke-WebRequest retrieves detailed HTTP response data, while Invoke-RestMethod is tailored for REST services, parsing JSON automatically. Usage example:
Invoke-RestMethod -Uri https://api.example.com/data -Method Get
Download the contents of a webpage:
Invoke-WebRequest -Uri "http://www.example.com" -OutFile "homepage.html"
Post data to a REST API and receive JSON response
Invoke-RestMethod -Uri "https://api.example.com/posts" -Method Post -Body '{"title":"Hello World","body":"This is a test post"}' -ContentType "application/json"
Firewall Management
The Get-NetFirewallRule and Set-NetFireFirewallRule cmdlets in PowerShell are integral tools for managing the Windows Firewall directly from the command line. They allow for viewing and modifying firewall rules, respectively. These cmdlets are part of the NetSecurity module in PowerShell.
Get-NetFirewallRule
Get-NetFirewallRule retrieves the firewall rules from the target computer’s current firewall policy. It allows administrators to inspect rules based on various criteria such as name, enabled state, action (allow or block), and more.
To retrieve All Firewall Rules:
Get-NetFirewallRule
Filter Firewall Rules by Name:
Get-NetFirewallRule -Name 'FPS-HTTP*'
List Only Enabled Firewall Rules:
Get-NetFirewallRule | Where-Object { $_.Enabled -eq 'True' }
Find Rules that are Blocking Traffic:
Get-NetFirewallRule | Where-Object { $_.Action -eq 'Block' }
Set-NetFirewallRule
Set-NetFirewallRule is used to modify the settings of existing firewall rules. This can include enabling or disabling rules, changing the action (allow or block), modifying the profiles it applies to, and more.
Disable a Specific Firewall Rule by Name:
Set-NetFirewallRule -Name "FPS-HTTP-In" -Enabled False
Change the Action of a Firewall Rule to Block:
Set-NetFirewallRule -Name "FPS-HTTP-In" -Action Block
Enable a Firewall Rule and Set it to Apply to Domain Profile:
Set-NetFireallRule -Name "FPS-HTTP-In" -Enabled True -Profile Domain
Conclusion
This guide demonstrates just a fraction of what PowerShell can achieve in network operations. With these cmdlets, administrators can automate tasks, thereby enhancing productivity and ensuring network stability and security. Both beginners and seasoned pros can benefit significantly from incorporating PowerShell into their toolkit.



