Search code examples
powershellencodingcharacter-encoding

Encoding csv webrequest


enter image description hereI'm having trouble being able to encode this request from google sheets.

$webresponse = Invoke-Webrequest -Uri "https://docs.google.com/spreadsheets/d/1hqjD2ghx-NNV6KgHqvPSvJoE5tQk6Sm_DJqyVHTrFwU/export?format=csv"
$News = $webresponse.Content  | ConvertFrom-Csv -Delimiter "," 

Anyway I spin it, The encoding isn't correct, primarily, apostrophes I noticed.

Any pointers would be greatly appreciated

Thank you


Solution

  • You can use [Encoding]::UTF8.GetString Method to avoid these encoding issues:

    $request = Invoke-WebRequest -Uri 'https://docs.google.com/spreadsheets/d/1hqjD2ghx-NNV6KgHqvPSvJoE5tQk6Sm_DJqyVHTrFwU/export?format=csv'
    [System.Text.Encoding]::UTF8.GetString($request.RawContentStream.ToArray()) | ConvertFrom-Csv
    

    Worth noting, in PowerShell 7.4+ this isn't an issue as it defaults to UTF8 Encoding.