# Ping Script help

**URL:** <https://forums.powershell.org/t/ping-script-help/4024>\
**Category:** PowerShell Help\
**Created:** [April 13, 2015, 11:00pm UTC](https://forums.powershell.org/t/ping-script-help/4024 "2015-04-13T23:00:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbie-boland](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@robbie-boland](https://forums.powershell.org/u/robbie-boland)\
**Post date:** [April 13, 2015, 11:00pm UTC](https://forums.powershell.org/t/ping-script-help/4024/1 "2015-04-13T23:00:36Z")

</div>

Good morning, I was trying to write a script that will get computer names and then would ping them to check if they are online.

`
#This script will get a list of names for all the computers in that room
$comps=Get-ADComputer -filter 'name -like "Building*roomnumber*"' | Select-Object -property name
foreach ($comp in $comps){
ping $comp}
`

When I run the above, I get:  
Ping request could not find host @{name=COMPNAME} Please check the name and try again. I thought it might just be ping, so i tried the Test-Connection cmdlet, but I get the same sort of problem.

How do I get a list of the computer names that will pipe into ping or test-connection so that I can see if the computers are online?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [April 13, 2015, 11:08pm UTC](https://forums.powershell.org/t/ping-script-help/4024/2 "2015-04-13T23:08:08Z")

</div>

First of all, you’ve got a logic error. Get-ADComputer produces objects of the type ADComputer; Select-Object is therefore producing “Selected” objects of that type. Those objects have a Name property, which contains a string. Ping.exe has no clue what to do with that, and PowerShell is likely producing a string representation like “@{name=COMPNAME}” which is why you’re seeing what you are.

In Select-Object, use -ExpandProperty instead of -Property. That will “extract” the string computer name value from the property, producing a simple string. “The Big Book of PowerShell Gotchas” has a bit on this, I think, and I go into it pretty well in Learn PowerShell in a Month of Lunches. Understanding the difference between an object that has a property which contains a string, and a string by itself, is a massively important concept in PowerShell.

* * *

Ping is probably not as easy to use as Test-Connection. But, look at the -Quiet parameter of Test-Connection, which makes it return just a True/False rather than a status.

```
if ((Test-Connection $comp -quiet)) { 
  # comp is pingable
} else {
  # comp is not pingable
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/ping-script-help/4024/3 "2024-05-16T20:45:30Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


