Powershell Export-CSV Tab Delimiter Output issue

66 Views Asked by At

Below is the Input CSV file which when after doing a sort and doing an export-csv , the format of the out does not looks good when opened.

$FileLogdate = (Get-Date -f yyyy-MM-dd_HH-mm-ss)

# Inputfile
"{0}`t{1}`t{2}" -f "COne","CTwo","CThree" | Out-File -Append $PSScriptRoot\testtrycsv_$FileLogdate.csv

"{0}`t{1}`t{2}" -f "C1R1","C2R1","C3R1" | Out-File -Append $PSScriptRoot\testtrycsv_$FileLogdate.csv
"{0}`t{1}`t{2}" -f "C1R1","C2R1","C3R1" | Out-File -Append $PSScriptRoot\testtrycsv_$FileLogdate.csv
"{0}`t{1}`t{2}" -f "C1R2","C2R2","C3R2" | Out-File -Append $PSScriptRoot\testtrycsv_$FileLogdate.csv
"{0}`t{1}`t{2}" -f "C1R1","C2R1","C3R1" | Out-File -Append $PSScriptRoot\testtrycsv_$FileLogdate.csv

# Output file
"{0}`t{1}`t{2}" -f "COne","CTwo","CThree" | Out-File -Append $PSScriptRoot\test-temp.csv

$input = "$PSScriptRoot\testtrycsv_$FileLogdate.csv"
$inputCsv = Import-Csv $input | Sort-Object * -Unique
$inputCsv | Export-Csv "$PSScriptRoot\test-temp.csv" -NoTypeInformation -delimiter "`t"

below is input csv file

Input CSV File when opened

Below is the output format of the export-csv view when opened

Format of file after export-csv

When looking the export-csv in Notepad++ , it looks like this

enter image description here

1

There are 1 best solutions below

2
Olaf On BEST ANSWER

I actually cannot reproduce the behaviour you're having. Let's create your input data ...

$CSVInput = @'
"COne","CTwo","CThree" 
"C1R1","C2R1","C3R1"
"C1R1","C2R1","C3R1"
"C1R2","C2R2","C3R2"
"C1R1","C2R1","C3R1"
'@ | 
    ConvertFrom-Csv

When we output this input data it looks like this:

PS:>$CSVInput

COne CTwo CThree
---- ---- ------
C1R1 C2R1 C3R1  
C1R1 C2R1 C3R1  
C1R2 C2R2 C3R2  
C1R1 C2R1 C3R1 

Now let's sort your input data ...

$SortedCSV =
    $CSVInput | 
        Sort-Object -Property * -Unique

When we output this sorted input data it looks like this:

PS:>$SortedCSV

COne CTwo CThree       
---- ---- ------       
C1R1 C2R1 C3R1
C1R2 C2R2 C3R2

Now let's export these sorted data to a file in the current working directory using a <tab> as delimiter ...

$SortedCSV |
    Export-Csv -Path '.\CSVOutput.csv' -Delimiter "`t" -NoTypeInformation

And the output of that looks like this ...

CSV file opened in Notepad++

¯\_(ツ)_/¯