Powershell is corrupting comma delimited files

New 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


This line is the original. No double quotes.



Here they are for back-to-back comparison




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.


This is the original line


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



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.
best response confirmed by JoyriderBC (New Contributor)

@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


Input file