Powershell - how to read Ansi CSV file

1.9k Views Asked by At

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?

The options for -Encoding are:

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

There are 1 best solutions below

3
mklement0 On BEST ANSWER

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

  • in Windows PowerShell:

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

    • In PowerShell v7.3-:

      -Encoding ([cultureinfo]::CurrentCulture.TextInfo.ANSICodePage)
      
    • In PowerShell v7.4+:[1]

      -Encoding Ansi
      

Default character encodings in the two PowerShell editions:

  • Windows PowerShell, the legacy, Windows-only, ships-with-Windows edition (whose latest and last version is v5.1.x), defaults to the active ANSI code page in key areas - notably Get-Content / Set-Content and when the PowerShell engine reads source code - but the defaults vary widely across the built-in cmdlets; case in point: Import-Csv defaults to UTF-8; see the bottom section of this answer for an overview.

  • PowerShell (Core), the modern, install-on-demand, cross-platform edition (which started with v6 and is currently at v7.2.x), now fortunately consistently defaults to (BOM-less) UTF-8.


[1] The absence of an Ansi -Encoding value in earlier PowerShell (Core) versions was a curious omission, given that an Oem value (for the active OEM code page) has always existed - see GitHub issue #6562 for the backstory.