PowerShell : Fancy Test-Connection

I say –  Let’s go all Microsoft ! 😀

Microsoft has enabled us to create our own Applications and Functionalities using Powershell, Moreover it made all our IT experiences manageable from a centralized console.

So idea of using a third party tool sounds a bit old fashioned 😛 to me , Lets make this Blog post a Good Bye to the Tools available over internet to Ping multiple computers in a ‘FANCY‘ manner and make   – ‘One of Our Own’ .


Tweak the Test-Connection cmdlet available in powershell and make it Presentable with extended functionalities such as ColorsHost Status, Success and Failure Percentage, Number of ICMP Attempts.


STEP 1 :  Create a function that accepts Values as an Parameter, just like other Cmdlets.


STEP 2 :  Initialize Array objects that would be used for the Calculation depending upon ICMP response.


STEP 3 :  Run an infinite loop and output the Header row on the screen


STEP 4 :  Now Pass each Hostname to the ForEach Block to test the Connectivity, Calculate and Display the coloured Output depending upon the ping response.



Please note, Quiet switch with Test-Connection only gives $true and $false values depending upon the Ping response.

Test-Connection $_  -Count 1 -Quiet

STEP 5 :  At the end please Pause the script execution for few seconds, so that screen doesn’t gets refreshed with the new values very quickly and are easy on eyes 🙂



Compile the function once and then you can run this Ping-Host utility in many ways, like in the screenshot below.



Post executing the script you will get coloured output depending upon the ping response you receive from the host.


Click on below link highlighted in GREEN to Auto-Download the complete Powershell script


Hopefully this script comes handy to you and useful in maintenance and other troubleshooting purposes.

Happy Reading Folks 🙂


8 thoughts on “PowerShell : Fancy Test-Connection

  1. Excellent multi host ping output table script. I’m surprised how well the cmdlet test-connection works as it is known to be less reliable than the .Net class, or WMI/CIM methods. You get an A+ in my book.


    1. Add the following to the function itself. The reason the If condition is less than or equal to 9 is because “$Total[$i]” is an array counting starting from zero.
      On line 39 insert:
      If($Total[$i] -le 9){

      On line 76 insert:
      } else { Break }

      Here is the entire function:

      #Function Ping-Host
      Function Ping-Host
      #Parameter Definition
      [Parameter(position = 0)] $Hosts,
      [Parameter] $ToCsv
      #Funtion to make space so that formatting looks good
      Function Make-Space($l,$Maximum)
      $space =””
      $s = [int]($Maximum – $l) + 1
      1..$s | %{$space+=” “}

      return [String]$space
      #Array Variable to store length of all hostnames
      $LengthArray = @()
      $Hosts | %{$LengthArray += $_.length}

      #Find Maximum length of hostname to adjust column witdth accordingly
      $Maximum = ($LengthArray | Measure-object -Maximum).maximum
      $Count = $hosts.Count

      #Initializing Array objects
      $Success = New-Object int[] $Count
      $Failure = New-Object int[] $Count
      $Total = New-Object int[] $Count
      #Running a never ending loop

      $i = 0 #Index number of the host stored in the array
      $out = “| HOST$(Make-Space 4 $Maximum)| STATUS | SUCCESS | FAILURE | ATTEMPTS |”
      If($Total[$i] -le 9){
      #output the Header Row on the screen
      Write-Host $Firstline
      Write-host $out -ForegroundColor White -BackgroundColor Black

      If(Test-Connection $_ -Count 1 -Quiet -ErrorAction SilentlyContinue)
      #Percent calclated on basis of number of attempts made
      $SuccessPercent = $(“{0:N2}” -f (($success[$i]/$total[$i])*100))
      $FailurePercent = $(“{0:N2}” -f (($Failure[$i]/$total[$i])*100))

      #Print status UP in GREEN if above condition is met
      Write-Host “| $_$(Make-Space $_.Length $Maximum)| UP$(Make-Space 2 4) | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|” -BackgroundColor Green

      #Percent calclated on basis of number of attempts made
      $SuccessPercent = $(“{0:N2}” -f (($success[$i]/$total[$i])*100))
      $FailurePercent = $(“{0:N2}” -f (($Failure[$i]/$total[$i])*100))

      #Print status DOWN in RED if above condition is met
      Write-Host “| $_$(Make-Space $_.Length $Maximum)| DOWN$(Make-Space 4 4) | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|” -BackgroundColor Red


      #Pause the loop for few seconds so that output
      #stays on screen for a while and doesn’t refreshes

      Start-Sleep -Seconds 1
      } else { Break }


  2. Thank you for another magnificent post. Where else may just anybody get that type
    of info in such an ideal means of writing? I have a presentation subsequent week, and I
    am on the look for such information.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s