The root problem: every command, a different encoding
In Windows PowerShell 5.1, almost every command has its own default encoding: Get-Content reads files without a BOM as ANSI (Windows-1252), Out-File and > write UTF-16, Set-Content writes ANSI and Export-Csv ASCII. PowerShell 7 unified this and uses UTF-8 without a BOM everywhere.
Before
User;Department;Office José GarcÃa;Sales;Málaga Renée Dubois;Purchasing;Zürich
After
User;Department;Office José García;Sales;Málaga Renée Dubois;Purchasing;Zürich
Read and write files correctly
Get-Content data.csv -Encoding UTF8reads UTF-8 files correctly even without a BOM.Set-Content,Out-FileandAdd-Contentwrite UTF-8 with-Encoding UTF8– in 5.1 always with a BOM.Export-Csv data.csv -Encoding UTF8 -NoTypeInformationinstead of the ASCII default, which replaces accented letters with?.- For all commands at once:
$PSDefaultParameterValues['*:Encoding'] = 'utf8'at the top of the script.
Web requests with Invoke-RestMethod and Invoke-WebRequest
If a server doesn’t state a character set in its Content-Type, Windows PowerShell 5.1 decodes the response as ISO-8859-1. UTF-8 responses then contain é. The fix: fetch the response as bytes and decode it yourself, for example with [Text.Encoding]::UTF8.GetString((Invoke-WebRequest $url).RawContentStream.ToArray()).
Script files with special characters
If you save a script as UTF-8 without a BOM, Windows PowerShell 5.1 reads it as ANSI. Characters in strings like Write-Host "Café" then show up as Café. Save .ps1 files as UTF-8 with a BOM – in VS Code via the encoding in the status bar, in Notepad++ via Encoding → UTF-8-BOM.
The console
If only the console window shows wrong characters, the file is often fine. Switch the output with [Console]::OutputEncoding = [Text.Encoding]::UTF8. For output you pipe to external programs, also set $OutputEncoding = [Text.Encoding]::UTF8.
If the file has already been written broken, drop it into the box above: Mojibuster repairs é and friends and converts ANSI files to UTF-8. If the file already contains question marks, the text is lost – see question marks instead of characters.
Frequently asked questions
Which PowerShell version do I have?
$PSVersionTable.PSVersion shows it. 5.1 is the Windows PowerShell built into Windows; from 7 on it’s just called PowerShell and uses UTF-8 by default.
Why does Excel open my CSV from PowerShell incorrectly?
PowerShell 7 writes UTF-8 without a BOM, which Excel doesn’t recognize. Use -Encoding utf8BOM or save the file with a BOM in Mojibuster. More on Excel showing special characters wrong.
Do I need chcp 65001?
Only for programs that follow the console code page. Within PowerShell, [Console]::OutputEncoding is enough.