Categories

How to Ping a Computer using PowerShell

Pinging a computer is really simple, and of course since this is PowerShell we don’t want to use something as primitive as ping.exe, we want to use .NET and take advantage of all the method calls and filters.

1
2
$ping = New-Object System.Net.NetworkInformation.Ping
$ping.Send("www.brangle.com")

We can also set a timeout so that our computer doesn’t hang to long on a server that won’t respond. Let’s add a timeout of a 5 seconds, we will first need to convert this to milliseconds, so that’s 5,000ms. Of course 5 seconds may not be a resonable timeout, you need to figure out what’s appropriate for you and what you’re willing to tolerate.

1
2
$ping = New-Object System.Net.NetworkInformation.Ping
$ping.Send("www.brangle.com", 5000)

Technical details: System.Net.NetworkInformation.Ping requires that you have .NET 2.0 or greater.

2 comments to How to Ping a Computer using PowerShell

Leave a Reply

eXTReMe Tracker