filter online workstations and make pssession

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 ?

To test a connection you would normally use Test-Connection. :wink:
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.