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

Python: Package Installer for Python (PIP)

This comes preinstalled with Python 3.4 or higher. Similar to the Microsoft PowerShell Gallery, Python.org…

PowerShell: Command Line to Empty a Remote Directory

PowerShell Method $remoteUncPath="\\FILESHERVER01\FOLDER01"$emptyDirectory="C:\emptyDirectory"md -Force $emptyDirectoryRemove-Item "$emptyDirectory`\*" -force -recurse -ErrorAction Continuerobocopy $emptyDirectory $remoteUncPath /mir /R:0 /W:0…

PowerShell: WinHTTP Proxy

The most direct method to set WinHTTP proxy settings on a Windows machine is to…