I want to export a list of Test-Connection results either to csv or txt file - either one is fine, but I'm not sure how to do it.
An alternative would be to display the 'online' results in green and 'offline' results in red.
This code shows a neat table that I would like to export to file as well. I've been trying for hours various iterations of export-csv and output file and the file is either empty or not created at all.
## read file and create array
$devices = Get-Content -Path C:\temp\devices.txt
## loop through each device
foreach($device in $devices)
{
$test = Test-Connection -ComputerName $device -Count 1 -Quiet
[pscustomobject]@{
DeviceName = $device
Status = if($test){'ONLINE'}
else{'OFFLINE'}
}
}
Result:

Another thing I tried is show the results in color like this:
## read file and create array
$devices = Get-Content -Path C:\temp\devices.txt
## loop through each device
foreach($device in $devices)
{
$test = Test-Connection -ComputerName $device -Count 1 -Quiet
[pscustomobject]@{
DeviceName = $device
Status = if($test){Write-Host "ONLINE" -ForegroundColor Green}
else{Write-Host "OFFLINE" -ForegroundColor Red}
}
}
But the resulting table formatting gets all messed up:
