Run continuous Ping with PowerShell

Publish Date: April 5, 2022

Run continuous Ping with PowerShell

Ever since the PowerShell is launched, people always tend to look for PowerShell alternatives for regular commands that we use in traditional command prompt (cmd.exe). For the record, every command-line tool that you used in command prompt also works in Windows PowerShell by default. But if you’re already on this page, I am guessing you might be interested in a hard-core alternative of traditional “Ping” command. Well, PowerShell offers a Test-Connection cmdlet to help you with the same thing plus much more.

The syntax of Test-Connection cmdlet is pretty straight-forward as shown below:

Test-Connection WebServer
Using Test-Connection as Ping alternative
Using Test-Connection as Ping alternative

You just type the cmdlet name followed by the name or IP address of remote computer. By default, it sends 4 packets (like the regular Ping command) and then stops. It also supports the -Count parameter to specify the number of packets but unfortunately, it doesn’t have any built-in parameter that you could use to run an endless (or continuous) ping like with “ping -t” command. Don’t worry, I will show you a way around this. To run a continuous ping, you need to use the following PowerShell command:

while($true){Test-Connection WebServer -Count 1; Start-Sleep -Seconds 1}

Hmmm… if this looks even more confusing, I do have another solution for you. You might want to create a New-PsPing module which will work like a traditional PowerShell cmdlet.

Note: In PowerShell 6.0 or higher (a.k.a. PowerShell Core), the Test-Connection cmdlet does have a -Continuous parameter built-in. By the way, it doesn’t come pre-installed with Windows so if you need PowerShell Core, you have to manually install it.

Creating New-PsPing Module

To create the new module, run the following command in PowerShell console:

ise $(New-Item $HOME\Documents\WindowsPowerShell\Modules\New-PsPing\New-PsPing.psm1 -Force)

This command will create a New-PsPing module file in your user’s module directory and then opens that file in PowerShell ISE. Now just copy the following code, paste it in PowerShell ISE and press Ctrl+S to save the file.

<#
.Synopsis
   Runs ping for connectivity checking to remote computer
.DESCRIPTION
   The New-PsPing cmdlet runs for connectivity checking to remote computer with a packet count of 4 by default. A non-stop ping is run if -Continuous switch is enabled.
.EXAMPLE
   New-PsPing -ComputerName FileServer
   This command runs a normal ping to remote FileServer with 4 packets similar to regular "ping" command.
.EXAMPLE
   New-PsPing -ComputerName WebServer -Continuous
   This command runs a continuous ping to remote WebServer similar to "ping -t" command. To stop this command, press "Ctrl+C" keys.
#>
function New-PsPing
{
    [CmdletBinding()]
    Param
    (
        # Specify remote computer name or IP address
        [Parameter(Mandatory=$true,
                   Position=0)]
        [Alias('Host', 'Target', 'IP')]
        $ComputerName,

        # Continuous switch parameter
        [switch]$Continuous
    )

    if($Continuous -eq $false){
            Test-Connection $ComputerName
       } else {
            while ($true) {Test-Connection $ComputerName -Count 1; Start-Sleep -Seconds 1}
       }
}

That’s it. Now launch a new PowerShell console and type Get-Help New-PsPing -Detailed command. You should see the detailed help of new PS module you just created including a few examples. See the following screenshot for reference:

Getting detailed help of New-PsPing module
Getting detailed help of New-PsPing module

To use this module, type New-PsPing -ComputerName techtutsonline.com and press Enter. By default, it will also send 4 packets and then stop. To run a continuous ping, you could use -Continuous switch as shown below:

New-PsPing -ComputerName techtutsonline.com -Continuous
Test New-PsPing Module with -Continuous switch
Using New-PsPing module with -Continuous switch

This command will now keep doing a continuous Ping to specified server or computer. To break it, press “Ctrl+C” keys. Look you just created a new PowerShell module that behaves and works just like a traditional PowerShell cmdlet. It has got its own help, examples, parameters. That was it for this tutorial. If you find this helpful, feel free to share it with others.



Microsoft Certified | Cisco Certified

Leave a Reply

back to top