Author Posts

October 23, 2017 at 12:18 pm

I have the code below where it grabs info from a CSV file. The problem is that, not everyone have a title, or a desk phone, or a cell phone. I'm able to write if title is NULL, don't read at line. This is working.

I was wondering if there's a way in PowerShell to tell it if any of these (Name, Title, Street, etc) is NULL, don't read that line?

I hope I'm being clear on what I'm asking.

  (Get-Content $html_files_location\$txt_file_names) `
     -creplace ("NAME",$employees.Name) `
      -creplace ("TITLE",$employees.Title) `
       -creplace ("STREET", $employees.Street) `
        -creplace ("ADDRESS_INFO", $employees.Address_info) `
         -creplace ("PHONE", $employees.Phone) `
          -creplace ("CELL", $employees.Cell) `
           -creplace ("WEBSITE", $web) `
            -creplace ("ADURL", $web.Trim("http://")) `
             -creplace ("LOGO", $logo) ` |
    
    Set-Content $html_files_location\$txt_file_names -Force

October 23, 2017 at 2:42 pm

Why aren't you using Import-Csv cmdlet?

October 23, 2017 at 3:55 pm

I am using that command. The above code is just a portion of what I have. I didn't copy the whole thing.

October 23, 2017 at 5:16 pm

If you're using Import-CSV, then each row is an object. Assuming you're enumerating the rows and using a variable like $row (since you didn't post that part, I can only provide a basic example) I'd probably do something like...

if ($row.website -and $row.name -and $row.title) {
# all three fields contain something
}

October 23, 2017 at 5:42 pm

Just be careful whether the value is NULL or just an empty string, they're not the same thing. You should pipe your row to Get-Member to check.

As for skipping rows that contain an empty cell, this should work:

$csv = Import-Csv test.csv

foreach ($row in $csv) {

    if (-not($row.PSObject.Properties.Value -contains '')) {

        Write-Output $row
  
    } #end if

} #end foreach

October 24, 2017 at 7:49 am

This should also work

$stripedcsv = Import-Csv C:\names\names.csv | ? { ($_.PSObject.Properties | % {$_.Value}) -notcontains "}