Home > PowerShell > how to ping computers using powershell

how to ping computers using powershell

I have written a small function for myself which I am using in all my scripts to check the availability of computer before acting on it. Here it is…

function ping-host {
param($computer)
$status = Get-WmiObject -Class Win32_PingStatus -Filter “Address=’$computer’”
if( $status.statuscode -eq 0) {
    return 1
}
else {
    return 0
 }
}

So, basically what this does is, returns 1 when the given computer is reachable or “0″ if it is not. Below is a small example to demonstrate the functionality.

if (ping-host mycomp) {

write-host “reachable”

}

else {

write-host “unreachable”

}

Let me know if you know any other simple way…

No related content found.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>