SOLVED

Powershell is corrupting comma delimited files

Copper Contributor

My PowerShell script is adding double quotes and formatting the text incorrectly.  I am removing lines that have a particular string.

This line created by PowerShell script

"RECTYPE,CNTBTCH,CNTITEM,IDVEND,IDINVC,TEXTTRX,DATEINVC,SWCALCTX,AMTGROSTOT,AMTINVCTOT"

This line is the original. No double quotes.

RECTYPE,CNTBTCH,CNTITEM,IDVEND,IDINVC,TEXTTRX,DATEINVC,SWCALCTX,AMTGROSTOT,AMTINVCTOT

 

Here they are for back-to-back comparison

“RECTYPE,CNTBTCH,CNTITEM,IDVEND,IDINVC,TEXTTRX,DATEINVC,SWCALCTX,AMTGROSTOT,AMTINVCTOT”

RECTYPE,CNTBTCH,CNTITEM,IDVEND,IDINVC,TEXTTRX,DATEINVC,SWCALCTX,AMTGROSTOT,AMTINVCTOT

 

The files are CSV but Excel puts the new file’s complete line in one cell as if it does not recognize a delimiter

This is the new line in Hex.

JoyriderBC_0-1624051322008.png

This is the original line

JoyriderBC_1-1624051448167.png

This is the .PS1

(get-content c:\exports\'Receiving Export SAGE.CSV'| select-string -pattern '   ,0,,,0,0' -notmatch) | Out-File c:\Exports\'Receiving Export SAGE Zeroes Gone.CSV'

 

5 Replies

@JoyriderBC 

 

Hi,
I try it from my side and its working perfect with no double quote when using the Out-File.
Are you saving the content from a custom PSObject.
I got the double quotes when I replace the Out-File with Export-CSV, and manage to remove it by using PowerShell 7
(get-content C:\Users\1.CSV ) | Export-Csv c:\Fixed.CSV -UseQuotes never

@farismalaeb The double quotes are minor problem.  It is the record corruption.  See the HEX dump where after every character there are unprintable codes.

Can you share part of the output file, just put dump data in it and let me have a closer look.
Thanks
best response confirmed by JoyriderBC (Copper Contributor)
Solution

@JoyriderBC You can specify the encoding of the files with the -Encoding parameter.

 

For example:

 

Out-File c:\Exports\'Receiving Export SAGE Zeroes Gone.CSV' -Encoding utf8

 

 

@diecknet Thank you.  Your suggestion fixed the two problems I was having.  The updated script put a blank line as the first line in the output file.  The input file does not have this blank line.  The import into my program fails.  How can the blank line be prevented?

Output file

JoyriderBC_0-1625519976161.png

Input file

JoyriderBC_1-1625520079164.png

 

1 best response

Accepted Solutions
best response confirmed by JoyriderBC (Copper Contributor)
Solution

@JoyriderBC You can specify the encoding of the files with the -Encoding parameter.

 

For example:

 

Out-File c:\Exports\'Receiving Export SAGE Zeroes Gone.CSV' -Encoding utf8

 

 

View solution in original post