# Create a variable for online and offline computers using test-connection

**URL:** <https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160>\
**Category:** PowerShell Help\
**Created:** [November 15, 2023, 10:49pm UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160 "2023-11-15T22:49:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sajith2702](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sajith2702](https://forums.powershell.org/u/sajith2702)\
**Post date:** [November 15, 2023, 10:49pm UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/1 "2023-11-15T22:49:58Z")

</div>

I am using the below script to capture the online and offline computers into different variables but I am getting only one computer name in each variable. What am I doing wrong. Can some one help please. I am new to powershell. Thanks.  
Script:  
$NOLaps | ForEach-Object {  
if (test-connection -computername $\_ -quiet -count 1) {  
new-object psobject -property @{ComputerName=$_} | Select-Object -ExpandProperty ComputerName -OutVariable Online  
}  
else {  
new-object psobject -property @{ComputerName=$_} | Select-Object -ExpandProperty ComputerName -OutVariable Offline  
}  
}

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 15, 2023, 10:55pm UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/2 "2023-11-15T22:55:52Z")

</div>

I’d recommend a little more streamlined approach … like this:

```auto
$ConnectionStatusList =
foreach ($ComputerName in $NOLaps) {
    [PSCustomObject]@{
        ComputerName = $ComputerName
        Online = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
    }
}
$ConnectionStatusList

```

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

---

<div class="post-metadata">

**Author:** ![sajith2702](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sajith2702](https://forums.powershell.org/u/sajith2702)\
**Post date:** [November 16, 2023, 10:22pm UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/3 "2023-11-16T22:22:16Z")

</div>

Hi Olaf. Here is the code in right format. My objective is to get all the online computers in one variable and offline computers in another variable. I am not getting any error but the variables are capturing only the last computer and skipping others. Trying to work out to get all the computers in the variables. I am not getting any error though. I can see the list of computers on the screen but the “Online” variable has only one computer object and it is the same case with “Offline” variable. Your help is appreciated.

```auto
$NOLaps | ForEach-Object {
if (test-connection -computername $_ -quiet -count 1) {
    new-object psobject -property @{ComputerName=$_} | Select-Object -ExpandProperty ComputerName -OutVariable Online
    }
else {
new-object psobject -property @{ComputerName=$_} | Select-Object -ExpandProperty ComputerName -OutVariable Offline
}
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 16, 2023, 11:01pm UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/4 "2023-11-16T23:01:03Z")

</div>

???  
🤷🏼‍♂️ 🤷🏼‍♂️

Have you actually read my reply? Have you tried my code suggestion? If not - why not?

> [@sajith2702](#):
>
> My objective is to get all the online computers in one variable and offline computers in another variable.

Why two separate variables? Why not one array containing all information you need? Try my suggestion.

**Edit:**

> [@sajith2702](#):
>
> the “Online” variable has only one computer object and it is the same case with “Offline” variable.

Let me guess … it’s always the last computer tested for both variables, right? 😏

You are overwriting the variables with each loop iteration. 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 17, 2023, 4:34am UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/5 "2023-11-17T04:34:49Z")

</div>

Change `-OutVariable Online` to `-OutVariable +Online`

The + appends to the variable

---

<div class="post-metadata">

**Author:** ![sajith2702](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sajith2702](https://forums.powershell.org/u/sajith2702)\
**Post date:** [November 18, 2023, 2:26am UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/6 "2023-11-18T02:26:08Z")

</div>

Thanks for you help krzydoug.

---

<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:14pm UTC](https://forums.powershell.org/t/create-a-variable-for-online-and-offline-computers-using-test-connection/23160/7 "2024-05-16T20:14:34Z")

</div>

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


