Help with creating csv file

Hello, I need help with outputting the information from a test-connection to a csv file. I would like the file to display the ip,result but it only displays the result to the file.

$computer = Get-Content "$PSScriptRoot\Computers.txt"

Test-Connection -ComputerName $computer -count 1 -Quiet | Out-File $PSScriptRoot\Results.csv

If you carefully (re-)read the help for Test-Connection you will learn that the cmdlet only returns True or False when you specify the parameter -Quiet. :wink:

So you have to omit the parameter -Quiet and you should use Export-Csv instead of Out-File for the output.

Try it his way:

$computer = Get-Content "$PSScriptRoot\Computers.txt"

Test-Connection -ComputerName $computer -Count 1  |
    Select-Object -Property Source, Destination, IPV4Address |
        Export-Csv -Path $PSScriptRoot\Results.csv 

Oh didn’t notice that. :sweat_smile:

Also, as is your code you gave me to try doesn’t export the info you put for Select-Object. I did run the code without Select-Object and notice that if a computer didn’t responded it wouldn’t export it. Probably because it didn’t responded/is an error. Would I need to create a forEach, if, else thing?

Also, how do I write it so when it exports the file it outputs the day it was ran, plus the amount of time it was ran that day. Something like Results_Today Date_00XX.csv when exported

Probably worth noting that Test-Connection has changed between powershell 5.1 and 7.2.2

and also doesn’t return a response if the host name is not found

This is powershell 7.2.2 (only one error)

PS C:\Users\*****\Desktop> Test-Connection somerubbishwebsite.com
Test-Connection: Testing connection to computer 'somerubbishwebsite.com' failed: Cannot resolve the target name.
PS C:\Users\*****\Desktop>

and on 5.1 you get 4 errors like this

Test-Connection : Testing connection to computer 'somerubbishwebsite.com' failed: No such host is known
At line:1 char:1
+ Test-Connection somerubbishwebsite.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (somerubbishwebsite.com:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

and nothing is sent down the pipeline, so use a try/catch and create a valid response to log in the CSV file