1

I have File.csv with the following headers:

Name,Active,Distance,Title,Host,Port,Type,Source,URL

There are hundreds of lines on this file, some with Active as "true" and some with Active as "False." My hope is to write an If statement based on each line. I've done this before with text files, but not with a csv. I have tried:

$csv = Import-Csv .\File.csv -Header @("Name","Active","Distance","Title","Host","Port","Type","Source,"URL")

Foreach ($line in $csv){
     If($_.Active -eq 'true'){
          Write-Host "$_.Name is Active"}
     else{
          Write-Host "$_.Name is Inactive"}
     }

When I do this, I get exactly this text listed for each line in the file

.Name is Inactive

If I change my Foreach statement to:


Foreach ($line in $csv){
     If($csv.Active -eq 'true'){
          Write-Host "$csv.Name is Active"}
     else{
          Write-Host "$csv.Name is Inactive"}
     }

I get this for every single line, and it's all the way to the right of the PowerShell window:

.Name is Active

I'm doing something very obviously wrong, by my Google searches are not helping... Anyone have a suggestion?

Christopher Cass
  • 817
  • 4
  • 19
  • 31
  • 3
    `"$($line.Name) is Active"` same for the `else` condition. The quotes will not allow the expansion of the property. Also, your `if` condition: `$csv.Active -eq 'true'`, you're referring to the object array instead of the item (`$line`) – Santiago Squarzon Apr 29 '22 at 16:59

0 Answers0