Home > Blockchain >  Powershell - how to read Ansi CSV file
Powershell - how to read Ansi CSV file

Time:10-18

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

CodePudding user response:

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:

    -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.


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.

  • Related