How to search imported CSV cell values for each row

Viewed 996

I'm attempting to import a csv, search the cell values(columns) in each row and then find and count any null or blank values. Then if the count hits 13, do X. However, when I run this code it appears the cell/column values are a single object and not the individual values for each column? How can I search the individual cell values in the row?

SAMPLE CODE

$DataFileLocation = "\\Server\Output.csv"
$sheet = import-csv $DataFileLocation
$count = 0
foreach ($row in $sheet) {
    foreach ($column in $row) {
        Write-Host "Searching value: $column"
        if ($column -eq $null -or " ") {
            Write-Host "Found a blank!"
            $count++
        }
    }
    $count
    if ($count -eq 13) {
        Write-Host "Found match!" -ForegroundColor Red
    }
    $count = 0
}
2 Answers

Each $row is a PSCustomObject:

PS > $row.GetType().Name
PSCustomObject

Access the underlying property values:

foreach ($column in $row.psobject.Properties.Value){
...
}

Check if $column is $null, empty or white space:

if ([System.String]::IsNullOrWhiteSpace($column)) { ... }

Or something like

$DataFileLocation = "\\Server\Output.csv"
$sheet = import-csv $DataFileLocation
$count = 0
foreach ($row in $sheet){
  ($row | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { 
    $n = $_.Name; "$n = $($row."$n")" 
  }
}
Related