A ping sweep is a network discovery technique used by network administrators to check the active IP addresses on the network. Use the PowerShell Test-Connection cmdlet to ping sweep to check whether the IP address is reachable.
Manually pinging IP addresses is a tiresome process and takes too much time. Using the PowerShell script with the Test-Connection cmdlet, it sends ICMP (Internet Control Message Protocol) to IP addresses and waits to get their reply.
In this article, we will discuss how to create a PowerShell script to ping sweep using the Test-Connection cmdlet.
PowerShell Ping Sweep using Test-Connection
To use the Test-Connection cmdlet, install the module “Microsoft.PowerShell.Management”. This command sends an ICMP echo request to the range of provided IP address and returns the result whether the target is reachable or not.
Install-Module -Name Microsoft.PowerShell.Management
Use the following PowerShell script to perform a ping sweep.
Import-Module Microsoft.PowerShell.Management $ips = 1..10 | % { "192.168.1.$_" } $ips | ForEach-Object { $result = Test-Connection -Count 1 -ComputerName $_ -Quiet "$($_) $result" }
In the above PowerShell script, it imports the required module “Microsoft.PowerShell.Management” that contains the Test-Connection cmdlet.
The $ips variable stores the specified IP address range from 192.168.1.1 to 192.168.1.10
To conduct a ping sweep in PowerShell, use the ForEach-Object
to iterate over the array of the specified IP address.
The Test-Connection cmdlet sends the ICMP echo request packets to each IP address to check if they are reachable or not. This cmdlet returns the status True if the IP address is reachable else False.
The output of the above PowerShell ping sweep script displays the IP address and its status True or False.
Cool Tip: How to use Test-Connection to ping a list of computers in PowerShell!
Conclusion
I hope the above article on how to conduct a ping sweep using the PowerShell Test-Connection cmdlet is helpful to you.
The results of ping sweep in PowerShell display IP address status whether they are active or not, it’s very useful for network administrators to troubleshoot network connectivity issues.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.