IP address status with output to CSV

Hello, Newbie here to Powershell.

I have a range of IP addresses that I’m currently getting their status. I would like to output the results to the Powershell window and to a CSV. Ideally the CSV would have two columns. One for the IP address and the other for its status which is setup for “is active” and “is not active”.
Any ideas on what I would need to add to the below code to achieve this?
Thank you

$ipRange = 1..510
$subnet = “192.168.2.”

foreach ($i in $ipRange) {
$ip = $subnet + $i
if (Test-Connection -ComputerName $ip -Count 1 -Quiet) {
Write-Host “$ip is active”
} else {
Write-Host “$ip is inactive”
}
}

AD1,
Welcome to the forum. :wave:t3:

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

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Your $ipRange will cause issues - you know that, right? :smirking_face:

$ipRange = 1..3
$subnet = '192.168.2.'
$Result = 
foreach ($i in $ipRange) {
    $ip = '{0}{1}' -f $subnet, $i
    [PSCustomObject]@{
        IP       = $ip
        IsActive = Test-Connection -ComputerName $ip -Count 1 -Quiet
    }
}
$Result
$Result | 
    Export-Csv -Path .\NetworkScan.csv -NoTypeInformation

Thanks for the catch on the $ipRange! I was thinking it would rollover to the next range. I will give your suggestion a try. I appreciate the help.

Since it does not know about IP ranges it wont. It’s just a range of integers from 1 to 510.