Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell – how to read Ansi CSV file

Notepad++ says the CSV file is Ansi encoded.

The Powershell 7 Import-CSV commandlet has various -Encoding options but ‘Ansi’ is not one of them.

How do I get Powershell to read this CSV without mangling it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The options for -Encoding are:

  • ascii
  • bigendianunicode
  • bigendianutf32
  • oem
  • unicode
  • utf7
  • utf8
  • utf8BOM
  • utf8NoBOM
  • utf32

>Solution :

To use ANSI encoding, i.e. the code page implied by the active legacy system locale (language for non-Unicode programs):

  • in Windows PowerShell:

    -Encoding Default
    
  • in PowerShell (Core) 7+, which you’re using, Default now refers to UTF-8, so more work is needed:

    -Encoding ([cultureinfo]::CurrentCulture.TextInfo.ANSICodePage)
    

The absence of an Ansi -Encoding value in PowerShell (Core) 7+ is a curious omission, given that an Oem value (for the active OEM code page) does exist – see GitHub issue #6562 for a discussion.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading