Powershell: Check if a Computer is Online

Fairly simple solution but this often comes up with others at work…Say you’ve got a piece of masterful automation with Powershell and need an easy way to check if a computer is online and execute code specifically for that. Well with the Test-Connection cmdlet and a few parameters we’re there.

In this example we’ll use a pre-populated array and run with it:

# Array of computers
$computerList = @("Computer1","Computer2","Computer3","Server1")# Foreach loop to test each connection

foreach ($computer in $computerList)
{
   If (Test-Connection -ComputerName $computer -Count 1 -Quiet)
   {
      Write-Host "$computer is Online"
      # Do some stuff....
   } else {
      Write-Host "$computer is offline"
      # No soup for you...
   }
}

The “-Count” parameter can of course be changed if more than one attempt may be necessary. The “-Quiet” parameter is used to suppress messages on the console. Technet article on Test-Connection.

You can take this a step futher and have the script import computers from a csv, database, or whatever else you feel.

-NDS

Advertisements

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