Script to ping multiple hosts with 'pingable' option

Hi I am very new to PowerShell and I wanted to create a script that would ping a list of hosts from a file and output the results into a file with the outcome. I’ve found this simple script online and it works but I need to have an outcome variant of ‘pingable’ which is when the CMD results in ‘request timed out’. Any help would be great. Thanks in advance.

$Output= @()
$names = Get-content "C:\hnames.txt"
foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
   $Output+= "$name,up"
   Write-Host "$Name,up"
  }
  else{
    $Output+= "$name,down"
    Write-Host "$Name,down"
  }
}
$Output | Out-file "C:\result.csv"
$names = Get-content -Path 'C:\hnames.txt'
$Output = foreach ($name in $names) {
    [PSCustomObject]@{
        ComputerName = $name
        Connected = Test-Connection -ComputerName $name -Quiet -Count 1
    }
}
$Output | Export-Csv -Path 'C:\result.csv' -NoTypeInformation

There are script that already exist for this use case on the MS PowerShellGallery and all over the web.

Just do a search for ‘powershell ping up down’

Examples;

PowerShell – IP to HostName along with Ping in Excel The script I am sharing today is extension of below script:PowerShell – Ping Machines and report in ExcelThis script will use list of IP address , pings them report if its up or down and query the Hostname from DNS... https://gallery.technet.microsoft.com/scriptcenter/PowerShell-IP-to-HostName-a856289b

PowerShell : Check if Machine is Up or Down

Here’s a simple get-ping function I wrote that only has a 100 ms timeout, and accepts piped input.

Function Get-Ping  {

  Param (
    [parameter(ValueFromPipeline)]
    [string[]]$Hostname='yahoo.com'
  )

  Begin {
    $Ping = New-Object System.Net.Networkinformation.ping
    $Timeout = 100 # ms
  }

  Process {
      $Ping.Send($hostname, $timeout) | Add-Member -passthru hostname $hostname[0] | 
      select hostname,address,status,roundtriptime
  }

}

So you can do:

get-content c:\hnames.txt | get-ping | select hostname,status | export-csv c:\result.csv
get-content c:\result.csv

#TYPE Selected.System.Management.Automation.PSCustomObject
"hostname","Status"
"COMP1","Success"
"COMP2","Success"
"COMP3","TimedOut"