Out-GridView shows an empty column where there is actual data

I have this CSV file.

Actual CSV data is limited to ";" , like this:

[2017-04-27 15:45:04] ;x;/x;Succes; 

You can see that there is a "Terminaison" column that contains information about success or failure. My goal is to display all the columns of a file in a gridview.

I do:

 import-csv "C:\x\Journal\Capsule\CapsulePoste.csv" -Delimiter ";" 

and get the desired result with all the columns containing the correct data:

Now here is my question: when I want to display all this in a GridView, is the "terminaison" column empty? What for? All other columns are displayed correctly ... :

 import-csv "C:\x\Journal\Capsule\CapsulePoste.csv" -Delimiter ";" | out-gridview 

I found that the white space at the end of the header (the first line of the CSV file) causes this ...

 Date;Trousse;Version;Demandeur;Action;Paramรจtres;Terminaison 

(there is a space immediately after Terminaison)

If I edit CSV in notepad and delete this empty space, bingo, it works. However, this does not solve my problem, since I do not want to edit the file in advance. What is this weird restriction and is there a workaround?


EDIT:

The answers below are excellent. I ended up using another option that I think should be added to the features:

 $content = Get-Content C:\x\Journal\Capsule\CapsulePoste.csv $content | Foreach {$_.TrimEnd()} | Set-Content C:\x\Journal\Capsule\CapsulePoste.csv import-csv "C:\x\Journal\Capsule\CapsulePoste.csv" -Delimiter ";"| sort-object Date -descending 
+7
powershell import-csv
source share
2 answers

Here is a general way to fix this after you have imported the CSV:

 $CSV = import-csv "C:\x\Capsule\CapsulePoste.csv" -Delimiter ";" $FixedObject = $CSV | ForEach-Object { $NewObject = New-Object -TypeName PSCustomObject $_.PSObject.Properties | ForEach-Object { $NewObject | Add-Member -Name $_.Name.Trim() -Value $_.Value -MemberType $_.MemberType } $NewObject } $FixedObject | Out-GridView 

In this case, iterates over the properties of each of the resulting objects, and then creates a new object with the same properties, but with the .Trim () method, launched by the name of each of them to remove any surrounding spaces.

This is obviously less efficient than just fixing the CSV file before importing, but it can (as a rule) also be difficult to do if you cannot be sure where the header appears in the CSV (as it is not always the first).

Of course, if you just want to fix this particular scenario, another answer is the simplest.

+3
source share

As I mentioned in the comments, there is an error in Out-GridView that displays empty values โ€‹โ€‹when there is a space or special character at the beginning or end of the property. I do not know how to fix the error with spaces, but you could fix the file before converting from CSV as follows:

 (Get-Content "C:\X\Capsule\CapsulePoste.csv") -replace 'Terminaison ','Terminaison' | ConvertFrom-CSV -Delimiter ";" | Out-GridView 

Note: as stated by Matt and Mark, Mark's answer is more general. This quick fix suggests that Terminaison is the title that needs to be fixed and not found anywhere in the document.

+6
source share

All Articles