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

Excel Visual Basic For Application (VBA): Determine IP List

Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True intRow = 2 Set Fso = CreateObject("Scripting.FileSystemObject") Set…

PowerShell: Get Available RAM Slots

# getRamSlotsAvailable.ps1 $computername=$env:computername function getRamSlotsAvailable{ param($computername=$env:computername) write-host "Computer name: $computerName" $slots = Get-WmiObject -Class "win32_PhysicalMemoryArray"…

PowerShell: Moving Virtual Machines & Expanding Disk Volumes in Hyper-V & Microsoft Failover Clusters

Sample VM Migation Plan (time window = 3 hours): Pre-emptively resolve disks merging errors prior…