Using Encoding Standards

by Mar 4, 2021

When working with text files, it is important to always use the same text encoding for reading and writing or else special characters can be damaged, or the text file may become unreadable.

In PowerShell 7, all cmdlets as well as the redirection operator use the default UTF8 text encoding unless you specify a different encoding. That’s good.

In Windows PowerShell, different cmdlets use different default encodings. Here, to establish a common sensible default encoding for all cmdlets and the redirection operator, you should set a default parameter value for any command with a parameter -Encoding.

Place the following line into your profile script:

$PSDefaultParameterValues.Add('*:Encoding', 'UTF8')

Also, in Windows PowerShell make sure you specify a distinct value whenever a cmdlet features the parameter -Encoding. The recommended encoding today is UTF8.

Twitter This Tip! ReTweet this Tip!