powershell snippet to test server names

I’ve been doing some scripting at work and had a desire to test if a server exists before attempting to do some work against it (less errors, cleaner execution…). I hadn’t found anything that I wanted to use so I asked in the #powershell channel on irc.freenode.net. MoW, of course, knew the answer since he is the Google of PowerShell. Give him a question and he’ll throw out the answer.

shell> $ping = new-Object System.Net.NetworkInformation.Ping
shell> $ping.Send(‘localhost’).status
Success
shell> $ping.Send(‘blah’).status
Exception calling “Send” with “1” argument(s): “An exception occurred during a Ping request.”

Update: Gaurhoth gives some information comparing Win32_PingStatus with the above method.