Posted On January 29, 2021

PowerShell: How to Quickly Ping a Target

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Quickly Ping a Target

Method 1: Legacy ping command

# Legacy method that works without any fusses
$pingResult=ping google.com -n 1
return [bool]($pingResult -match '0% loss')

Method 2: Use WMI

# This ping command would limit the ttl to a certain value
# WMI must be healthy on the host system for this to work
$remoteHost='google.com'
$timeOut=200
[bool]$pingable=!(!(Get-WmiObject Win32_PingStatus -Filter "Address='$remoteHost' and Timeout=$timeout and ResolveAddressNames='true' and StatusCode=0"))
if($pingable){write-host "Ping $remotehost successful"}else{write-host "Ping $remoteHost failed"}

Method 3: Use PowerShell Native

# This works most of the time. On certain computers with corrupted WMI, this would freeze indefinitely
Test-connection google.com -Count 1 -Quiet

Leave a Reply

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

Related Post

How To Recover SQLSERVER Service from Being Unable to Start

# startSqlService.ps1 # special provision to deal with SQL Server not starting up due to…

PowerShell: Installing or Including an Application On a Computer or Scripting Session

Sample Usage: PS C:\WINDOWS\system32> includeapp -appName notepadplusplus -appExe notepad++notepadplusplus version 7.91.0.0 already exists.True function includeApp($appName,$appExe=$False,$version){…

Function to Import PortQry (a Systernal Utility)

Update: much adieu about nothing. Run this quick script and ignore the prior jibberish: if…