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

Update Windows with Restricted Internet Access

This script is an extract of a longer version with the features of simultaneous executions…

PowerShell: Rename Photo and Video Files by Adding Creation Dates

Have you ever downloaded a punch of images, MOV, and MP4 from iCloud to find…

List Autorun Services

# $cred = get-Credential -credential kdoan-a $Username = 'kimconnect\kim-Admin $Password = 'PASSWORD' $pass = ConvertTo-SecureString…