PowerShell for Windows Admins

May 16 2011   2:56PM GMT

Scripting Games Commentary: XIII ping

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


How many times have you done this when testing network connectivity?


If all we want is a test of connectivity ping works fine. We can ping using WMI or .NET classes from within PowerShell. This has the advantage of returning an object rather than text which means we can work with it much easier.

Using .NET

$ping = New-Object -TypeName System.Net.NetworkInformation.Ping

This could be condensed to

$ping = (New-Object -TypeName System.Net.NetworkInformation.Ping).Send(“”)

and we get the results in the ping variable.

The WMI solution is like this

Get-WmiObject -Class Win32_PingStatus -Filter “Address=’′”

We can make this even easier because PowerShell 2 wraps this WMI class in a cmdlet

Test-Connection -ComputerName

if you don’t want to type all that then create an alias

Set-Alias -Name p -Value Test-Connection

just don’t use the alias in scripts

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: