# filter online workstations and make pssession

**URL:** <https://forums.powershell.org/t/filter-online-workstations-and-make-pssession/14478>\
**Category:** PowerShell Help\
**Created:** [June 4, 2020, 4:37am UTC](https://forums.powershell.org/t/filter-online-workstations-and-make-pssession/14478 "2020-06-04T04:37:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlsalex](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@tlsalex](https://forums.powershell.org/u/tlsalex)\
**Post date:** [June 4, 2020, 4:37am UTC](https://forums.powershell.org/t/filter-online-workstations-and-make-pssession/14478/1 "2020-06-04T04:37:21Z")

</div>

Get-ADComputer -Filter \* -SearchBase "OU=Systems,OU=Alex,DC=com,DC=cn  
I can filter out all computer in the OU of Systems,

Get-ADComputer -Filter \* -SearchBase “OU=Systems,OU=Alex,DC=com,DC=cn” | foreach {Test-WSMan -ComputerName $\_.DNSHostName}  
Then I know which computer is online.

Then I want to use new-pssession to create a session for every online computer only.  
but how to do that in the pile after I test the computer. I want to complete my request in one line by using pile.  
I also want to add the DNSHostName to an additional text file for the on line computer by using \> textfile.txt after test-wsman  
any good idea ?

---

<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:** [June 4, 2020, 4:59am UTC](https://forums.powershell.org/t/filter-online-workstations-and-make-pssession/14478/2 "2020-06-04T04:59:49Z")

</div>

To test a connection you would normally use [Test-Connection](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-7). 😉  
With that you can use an if statement to check the availability of the remote computers and when they are online you can use New-PSSession in your Foreach-Object loop.

BTW:When you post code, error messages, sample data or console output format it as code, please.  
In the “ **Text** ” view you can use the code tags “ **PRE** ”, in the “ **Visual** ” view you can use the format template “ **Preformatted** ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.  
Thanks in advance.

---

<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:29pm UTC](https://forums.powershell.org/t/filter-online-workstations-and-make-pssession/14478/3 "2024-05-16T20:29:27Z")

</div>


