# Test-Connection

**URL:** <https://forums.powershell.org/t/test-connection/6669>\
**Category:** PowerShell Help\
**Created:** [June 20, 2016, 12:56pm UTC](https://forums.powershell.org/t/test-connection/6669 "2016-06-20T12:56:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 20, 2016, 12:56pm UTC](https://forums.powershell.org/t/test-connection/6669/1 "2016-06-20T12:56:10Z")

</div>

I run this script and I want it to tell me the name of the failed ping. Where “$computer is offline” is, I want it to show “pc2 is offline” or whatever pc fails the ping basically. Any ideas?

$csv = Get-Content pclist.csv

```
foreach ($computer in $csv)
{

```

try  
{  
Test-Connection $computer -Count 1 -ErrorAction Stop | Select-Object Address, IPV4Address  
}  
catch [System.Net.NetworkInformation.PingException]  
{  
‘$computer is offline.’  
}  
}  
pause

I’ll end with a report that looks like this

Address IPV4Address

* * *

pc0 192.168.1.1  
pc1 192.168.1.2  
$computer is offline.

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [June 20, 2016, 1:04pm UTC](https://forums.powershell.org/t/test-connection/6669/2 "2016-06-20T13:04:51Z")

</div>

‘comp1’,‘comp2’ | % {[pscustomobject]@{online = [bool](test-connection $\_ -Count 1 -quiet);computername = $\_}}

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 20, 2016, 1:08pm UTC](https://forums.powershell.org/t/test-connection/6669/3 "2016-06-20T13:08:24Z")

</div>

I apologize, I am very new to powershell, is this line replacing something or where specifically am i placing it?

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [June 20, 2016, 1:18pm UTC](https://forums.powershell.org/t/test-connection/6669/4 "2016-06-20T13:18:00Z")

</div>

Pinging computers is every beginners favorite thing to do:-) I suggest browsing the the technet script repository here.

[https://gallery.technet.microsoft.com/scriptcenter](https://gallery.technet.microsoft.com/scriptcenter)

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 20, 2016, 1:25pm UTC](https://forums.powershell.org/t/test-connection/6669/5 "2016-06-20T13:25:46Z")

</div>

Thanks! I will look into it. Appreciate the responses. I will see what I can do.

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [June 20, 2016, 1:34pm UTC](https://forums.powershell.org/t/test-connection/6669/6 "2016-06-20T13:34:25Z")

</div>

```
foreach($computer in $computers){

$test = test-connection $computer -Count 1 -ea 'silentlycontinue'

[pscustomobject]@{

status = if($test){'online'}else{'offline'}
ipv4 = $test.ipv4address.ipaddresstostring
name = $computer

}

}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [June 20, 2016, 1:47pm UTC](https://forums.powershell.org/t/test-connection/6669/7 "2016-06-20T13:47:06Z")

</div>

```
'$computer is offline.'
```

This would not work if you want variable expansion. You would need to use double quotes for that.

Here’s another example of a one liner…

```
Get-Content pclist.csv | Foreach-Object { If (Test-Connection -ComputerName $_ -Quiet -Count 1) { Write-Host "$_ is online" -ForegroundColor Green } Else { Write-Host "$_ failed ping" -ForegroundColor Red" } }
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [June 20, 2016, 1:50pm UTC](https://forums.powershell.org/t/test-connection/6669/8 "2016-06-20T13:50:02Z")

</div>

You just killed four puppies:-)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [June 20, 2016, 2:10pm UTC](https://forums.powershell.org/t/test-connection/6669/9 "2016-06-20T14:10:13Z")

</div>

Why would you have me do such a thing? lol

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 20, 2016, 2:16pm UTC](https://forums.powershell.org/t/test-connection/6669/10 "2016-06-20T14:16:15Z")

</div>

Ahhh, Thanks! That was my issue. lol

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [June 21, 2016, 3:32am UTC](https://forums.powershell.org/t/test-connection/6669/11 "2016-06-21T03:32:58Z")

</div>

To make life easier with Dan’s excellent piece of code you can wrap a small function around it:

```
function test-ping {
    
    $args | % {[pscustomobject]@{online = [bool](test-connection $_ -Count 1 -quiet);computername = $_}}
}
```

Then you can just type

```
Test-ping Device1 Device2
```

---

<div class="post-metadata">

**Author:** ![nigel-tatschner](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nigel-tatschner/32/312_2.png) [@nigel-tatschner](https://forums.powershell.org/u/nigel-tatschner)\
**Post date:** [June 21, 2016, 5:58am UTC](https://forums.powershell.org/t/test-connection/6669/12 "2016-06-21T05:58:28Z")

</div>

“Test-Connection -ComputerName SERVER -Quite” returns a System.Boolean Type,  
basically a “True” or “False”

You could use it like;

```
$Import = Get-Content -Path C:\ServerList.txt

foreach ($a in $Import) {
    if (Test-Connection -ComputerName $a -Count 2 -Quite) { 
            Write-Host "Ping Succeeded" 
            } Else {
              Write-Host "Ping Failed"
            }
}
```

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [June 21, 2016, 8:13am UTC](https://forums.powershell.org/t/test-connection/6669/13 "2016-06-21T08:13:56Z")

</div>

Yes Nigel you are quite right, no requirement for the [bool] type.

So as a short function :

```
function test-ping { $args | % {[pscustomobject]@{online = test-connection $_ -Count 1 -quiet;computername = $_}} }

#run
Test-ping Device1 Device2
```

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 21, 2016, 12:04pm UTC](https://forums.powershell.org/t/test-connection/6669/14 "2016-06-21T12:04:32Z")

</div>

I really like the output and speed of Grahams edit of Nigel’s script! I wish it could show ipv4 and servicetag info as well. How would I pipe these into the script? Without the need for the txt/csv. Also I noticed it truncates the list of my ipv4 addresses with my original script which was annoying not to see the last octet of the contacted host. The last script listed seems to have a different list view output.

$a = Get-Content “C:\Powershell\Computers.txt”

foreach ($i in $a)  
{$i + “`n” + “==========================”; Get-WmiObject win32\_SystemEnclosure -computername $i | select serialnumber}

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [June 22, 2016, 5:20am UTC](https://forums.powershell.org/t/test-connection/6669/15 "2016-06-22T05:20:26Z")

</div>

Hi Brad,

This is a bit more code but should do what you require :

```
function test-ping {
    param([Object[]]$Devices)        
    
            foreach ($Device in $Devices) {

                if (test-connection $Device -Count 1 -quiet) {
    
                    [pscustomobject]@{

                    computername = $Device 
                    online = "True"
                    IPaddress = (test-connection $Device -Count 1).IPV4Address.IPAddressToString
                    serialnumber = (Get-WmiObject -ComputerName $device win32_SystemEnclosure).serialnumber
                    }

                } else {
                    
                    [pscustomobject]@{

                    computername = $Device 
                    online = "false"
                    IPaddress = " `t- "
                    serialnumber = " `t-"
                    }
                }
        }
}
```

let me know.

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 22, 2016, 1:17pm UTC](https://forums.powershell.org/t/test-connection/6669/16 "2016-06-22T13:17:30Z")

</div>

I got it to work by calling the CSV and pointing the foreach to it. The only issues I have are the truncating of the tables and after about 15 down the list is stalls and never moves to the next computer. I don’t see where I would specify the Devices to test-ping on without calling the csv though. Thanks Graham!

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 22, 2016, 1:21pm UTC](https://forums.powershell.org/t/test-connection/6669/17 "2016-06-22T13:21:27Z")

</div>

Output example

computername online IPaddress serialnumber

* * *

Device1 Online 10.10.0.2 None  
Device2 Online 10.10.0.3 None  
Device3 Online 192.16… 30254V1  
Device4 Off… - -  
Device5 Online 10.10.0.4 None

---

<div class="post-metadata">

**Author:** ![brad-lankford](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@brad-lankford](https://forums.powershell.org/u/brad-lankford)\
**Post date:** [June 22, 2016, 1:30pm UTC](https://forums.powershell.org/t/test-connection/6669/18 "2016-06-22T13:30:27Z")

</div>

Actually after hanging a while it threw this and didn’t display the serial and loaded most of the rest of the list fine. So that’s the issue. May be worthy to note that the errors are only thrown in windows 7 workstations and none of the Servers had the error.

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)  
At C:\Users\user\Desktop\ps\New Text Document (2).ps1:15 char:37

- … alnumber = (Get-WmiObject -ComputerName $device win32\_SystemEnclosure …
- 

```
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidOperation: (🙂 [Get-WmiObject], COMException
  - FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [June 23, 2016, 2:19am UTC](https://forums.powershell.org/t/test-connection/6669/19 "2016-06-23T02:19:33Z")

</div>

Hi Brad,

So i’ve amended the code to look at WMI class Win32\_Bios for the serial number instead of, win32\_SystemEnclosure. Win32\_Bios might be more generic for you:

```
function test-ping {
    param([Object[]]$Devices)        
    
            foreach ($Device in $Devices) {

                if (test-connection $Device -Count 1 -quiet) {
    
                    [pscustomobject]@{

                    computername = $Device 
                    online = "True"
                    IPaddress = (test-connection $Device -Count 1).IPV4Address.IPAddressToString
                    serialnumber = (Get-WmiObject -ComputerName $device win32_bios).SerialNumber
                    }

                } else {
                    
                    [pscustomobject]@{

                    computername = $Device 
                    online = "false"
                    IPaddress = " `t- "
                    serialnumber = " `t-"
                    }
                }
        }
}
```

Do you have a list of PC’s in a txt or csv file ?

If you have them in a file, you can use whats called a subexpression, $().

So your’s would look like this:

```
test-ping $(get-content c:\temp\PCs.csv)
```

Or if you just want to pass a couple to test-ping:

```
test-ping PC1, PC2, PC3
```

Does that help ?

---

<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:41pm UTC](https://forums.powershell.org/t/test-connection/6669/20 "2024-05-16T20:41:02Z")

</div>


