Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Test Subnet with PowerShell

Posted on April 25, 2014

keyboardanalyzeA few years ago I published a PowerShell function to test IP addresses on a given subnet. I had an email the other day about it and I decided to refresh it. My new version adds a few bells and whistles that I think you might like. For example, you can now run it from a remote computer. In other words, you can ping IP addresses from another computer which might be helpful.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

I've also added an option to resolve the IP address to a hostname using DNS. Because I don't want to require anything on a remote computer, I am using the .NET DNS class to resolve the name. In addition, I have included a fallback resolution using NETBIOS. If you ask for resolution, and DNS doesn't return a name, you can opt to use the NBTSTAT command to resolve the hostname. I use a regular expression pattern to pull the computername.

Here's version 2.0 of Test-Subnet.

#requires -version 3.0

Function Test-Subnet {

<#
.SYNOPSIS
Ping addresses in an IP subnet
.DESCRIPTION
This command is a wrapper for Test-Connection. It will ping an IP subnet range
and return a custom object for each address indicating if the address responded
to a ping.

IPAddress      : 172.16.10.1
Hostname       : 
Pinged         : True
TTL            : 80
Buffersize     : 32
Delay          : 1
TestDate       : 4/25/2014 9:53:08 AM
PSComputerName : JH-WIN81-ENT
RunspaceId     : ac86e6eb-676f-4e80-b231-4b554e2e5039

By default the command pings all hosts from 1 to 254 on the specfied subnet. 
Enter the subnet value like this: 172.16.10.0.

You can optionally choose to resolve the IP address to a hostname using DNS
with a last resort, if that fails, to use NETBIOS.

.PARAMETER Subnet
The IP subnet such as 192.168.10.0. A regular expression pattern will validate
the subnet value.
.PARAMETER Range
The range of host IP addresses. The default is 1..254.
.PARAMETER Count
The number of pings to send. The default is 1. The most
you can send with this command is 10.
.PARAMETER Delay
The delay between pings in seconds. The default is 1. The maximum value is 60.
.PARAMETER Buffer
Specifies the size, in bytes, of the buffer sent with this command.
The buffer default is 32.
.PARAMETER TTL
Specifies the maximum time, in seconds, that each echo request packet 
("pings") is active. The default value is 80 (seconds). 
.PARAMETER AsJob
Run the command as a background job.
.PARAMETER Resolve
Resolve the DNS host name if the computer can be pinged.
.PARAMETER UseNBT
If the host name cannot be resolved using DNS, attempt to resolve using NETBIOS
and the NBTSTAT command.
.PARAMETER Computername
Test the subnet from a remote computer. The default is the local host.
The remote computer should be running PowerShell v3 or later but it is not
required as long as remoting is enabled.
.EXAMPLE
PS C:\> Test-Subnet 192.168.10.0
Ping all computers in the 192.168.10 subnet.
.EXAMPLE
PS C:\> Test-Subnet 192.168.10.0 (100..200) -asjob
Ping computers 192.168.10.100 through 192.168.10.200 and run the command as a 
background job.
.NOTES
NAME        :  Test-Subnet
VERSION     :  2.0   
LAST UPDATED:  4/25/2014
AUTHOR      :  Jeffery Hicks

Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell 3 in a Month of Lunches (http://manning.com/jones3/)
 Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************

Originally published https://jdhitsolutions.com/blog/2011/11/ping-ip-range/

.LINK
https://jdhitsolutions.com/blog/2014/04/test-subnet-with-powershell/
.LINK
Test-Connection 
.INPUTS
None
.OUTPUTS
Custom object
#>

[cmdletbinding(DefaultParameterSetName="NoResolve")]

Param (
[Parameter(Position=0)]
[ValidatePattern("\d{1,3}\.\d{1,3}\.\d{1,3}\.0")]
[string]$Subnet="172.16.10.0",
[Parameter(Position=1)]
[ValidateRange(1,254)]
[int[]]$Range=1..254,
[ValidateRange(1,10)]
[int]$Count=1,
[ValidateRange(1,60)]
[int]$Delay=1,
[ValidateScript({$_ -ge 1})]
[int]$Buffer=32,
[ValidateScript({$_ -ge 1})]
[int]$TTL=80,
[Switch]$AsJob,
[Parameter(ParameterSetName="Resolve")]
[Switch]$Resolve,
[Parameter(ParameterSetName="Resolve")]
[Switch]$UseNBT,
[ValidateNotNullorEmpty()]
[string[]]$Computername=$env:COMPUTERNAME
)

Write-Verbose "Testing $subnet"
#define a scriptblock so we can run as a job if necessary
$sb={

#define some variables for Write-Progress
$progHash=@{
Activity = "Test Subnet from $($env:computername)"
Status = "Pinging"
CurrentOperation = $Null
PercentComplete = 0
}

Write-Progress @progHash

$i=0
$total = ($using:Range).count

Foreach($node in ($using:range)) {

    $i++
    
    $progHash.PercentComplete = ($i/$total) * 100

    #replace the 0 with the range number
    $target= ([regex]"0$").replace($using:subnet,$node)

    $progHash.CurrentOperation = $target
    Write-Progress @progHash
    
    #define a hashtable of paramters to splat to Test-Connection
    $pingHash = @{
      ComputerName = $target
      count = $using:count
      Delay = $using:delay
      BufferSize = $using:Buffer
      TimeToLive = $using:ttl
      Quiet = $True
    }

    $ping = Test-Connection @pingHash 
    
    if ($ping -AND $using:resolve) {
        $progHash.status =  "Resolving host name"
        Write-Progress @progHash
        <#
        using .NET because there's no guarantee remote computers
        will have the necessary cmdlets, and this should also be
        faster.
        #>
        $Hostname = [system.net.dns]::Resolve("$target").hostname

        if ($UseNBT -AND ($hostname -eq $target)) {
            Write-verbose "Resolving with NBTSTAT"
            [regex]$rx="(?<Name>\S+)\s+<00>\s+UNIQUE"                                                              
            $nbt = nbtstat -A $target | out-string
            $Hostname = $rx.Match($nbt).groups["Name"].value    

        }
    }
    else {
        $Hostname = $Null
    }

    #use an ordered hashtable if running v3 or later
    if ($PSVersionTable.PSVersion.Major -ge 3) {
        $resultHash = [ordered]@{
            IPAddress = $Target
            Hostname = $Hostname
            Pinged = $ping
            TTL = $using:TTL
            Buffersize = $using:buffer
            Delay = $Using:Delay
            TestDate = Get-Date
        }
    }
    else {
       $resultHash = @{
            IPAddress = $Target
            Hostname = $Hostname
            Pinged = $ping
            TTL = $using:TTL
            Buffersize = $using:buffer
            Delay = $Using:Delay
            TestDate = Get-Date
        }
    }

    #create the property
    New-Object -TypeName PSObject -Property $resultHash
}
} #close scriptblock


#hashtable of parameters for Invoke-Command
$icmHash = @{
  Scriptblock = $sb
  Computername = $Computername
}
if ($AsJob) {
    Write-Verbose "Creating a background job"
    #Start-Job -ScriptBlock $sb -Name "Ping $subnet" 
    $icmHash.Add("AsJob",$True)
    $icmHash.Add("JobName","Ping $subnet") 
}
 Write-Verbose "Running the command"
 Invoke-Command @icmHash 
 
} #end function

And to be clear, "subnet" may be a bit of a misnomer as I'm not calculating any addresses with a subnet mask. Instead you enter the base IPAddress, like 10.10.1.0 and then a range of host numbers between 1 and 254, which is the default by the way. The command will then test 10.10.1.1 through 10.10.1.254, or whatever you entered.

My script employs some other techniques you might find help such as splatting, parameter sets, Write-Progress and parameter validation. Here's a screenshot of results that I've sent to Out-Gridview and then customized.

test-subnet

But since the command writes objects to the pipeline you could do whatever you wanted. I trust you'll let me know what you think. Enjoy!!


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Test Subnet with PowerShell”

  1. howfunky says:
    April 30, 2014 at 12:33 pm

    Jeff,
    Can I bug you to get it to support IPv6? Happy to help you with some of the IPv6 specific parts if you want 😉
    – Ed

    1. Jeffery Hicks says:
      April 30, 2014 at 12:40 pm

      I guess this would give me a reason to really learn IPv6.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d