Skip to content

Commit 4303089

Browse files
authored
Update $OutputEncoding (#10877)
Updates the documentation for the $OutputEncoding preference variable. This also updates the examples to correctly reflect what this option actually does.
1 parent 2a61046 commit 4303089

File tree

5 files changed

+173
-135
lines changed

5 files changed

+173
-135
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Preference_Variables.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -978,12 +978,12 @@ Remove-Variable OFS
978978

979979
## $OutputEncoding
980980

981-
Determines the character encoding method that PowerShell uses when it sends
982-
text to other applications.
981+
Determines the character encoding method that PowerShell uses when piping data
982+
into native applications.
983983

984-
For example, if an application returns Unicode strings to PowerShell, you might
985-
need to change the value to **UnicodeEncoding** to send the characters
986-
correctly.
984+
> [!NOTE]
985+
> In the majority of scenarios, the value for `$OutputEncoding` should align
986+
> to the value of `[Console]::InputEncoding`.
987987
988988
The valid values are as follows: Objects derived from an Encoding class, such
989989
as [**ASCIIEncoding**][60],
@@ -996,51 +996,57 @@ as [**ASCIIEncoding**][60],
996996

997997
### Examples
998998

999-
This example shows how to make the Windows `findstr.exe` command work in
1000-
PowerShell on a computer that's localized for a language that uses Unicode
1001-
characters, such as Chinese.
1002-
1003999
The first command finds the value of `$OutputEncoding`. Because the value is an
10041000
encoding object, display only its **EncodingName** property.
10051001

10061002
```powershell
10071003
$OutputEncoding.EncodingName
10081004
```
10091005

1010-
In this example, a `findstr.exe` command is used to search for two Chinese
1011-
characters that are present in the `Test.txt` file. When this `findstr.exe`
1012-
command is run in the Windows Command Prompt (`cmd.exe`), `findstr.exe` finds
1013-
the characters in the text file. However, when you run the same `findstr.exe`
1014-
command in PowerShell, the characters aren't found because the PowerShell sends
1015-
them to `findstr.exe` in ASCII text, instead of in Unicode text.
1006+
The remaining examples use the following PowerShell script saved as
1007+
`hexdump.ps1` to illustrate the behavior of `$OutputEncoding`.
10161008

10171009
```powershell
1018-
findstr <Unicode-characters>
1010+
$inputStream = [Console]::OpenStandardInput()
1011+
try {
1012+
$buffer = [byte[]]::new(1024)
1013+
$read = $inputStream.Read($buffer, 0, $buffer.Length)
1014+
$actual = [byte[]]::new($read)
1015+
[Array]::Copy($buffer, $actual, $read)
1016+
Format-Hex -InputObject $actual
1017+
} finally {
1018+
$inputStream.Dispose()
1019+
}
10191020
```
10201021

1021-
To make the command work in PowerShell, set the value of `$OutputEncoding` to
1022-
the value of the **OutputEncoding** property of the console, that's based on
1023-
the locale selected for Windows. Because **OutputEncoding** is a static
1024-
property of the console, use double-colons (`::`) in the command.
1022+
The following example shows how the string value `café` is encoded to bytes
1023+
when piped into `hexdump.ps1` created above. It demonstrates that the string
1024+
value is encoded using the `windows-1252` encoding scheme which is the default
1025+
encoding on the system tested in question.
10251026

10261027
```powershell
1027-
$OutputEncoding = [console]::OutputEncoding
1028-
$OutputEncoding.EncodingName
1028+
'café' | powershell.exe -File .\hexdump.ps1
10291029
```
10301030

10311031
```Output
1032-
OEM United States
1032+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
1033+
1034+
00000000 63 61 66 3F 0D 0A caf?..
10331035
```
10341036

1035-
After the encoding change, the `findstr.exe` command finds the Unicode
1036-
characters.
1037+
The following example shows how the bytes change when changing the encoding
1038+
to UTF-8. The `é` instead of being encoded to `0x3F` as done by `windows-1252`
1039+
it will now become `0xC3 0xA9` due to the UTF-8 encoding being used.
10371040

10381041
```powershell
1039-
findstr <Unicode-characters>
1042+
$OutputEncoding = [System.Text.UTF8Encoding]::new()
1043+
'café' | powershell.exe -File .\hexdump.ps1
10401044
```
10411045

10421046
```Output
1043-
test.txt: <Unicode-characters>
1047+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
1048+
1049+
00000000 63 61 66 C3 A9 0D 0A café..
10441050
```
10451051

10461052
## $ProgressPreference

reference/7.2/Microsoft.PowerShell.Core/About/about_Preference_Variables.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -866,12 +866,12 @@ Remove-Variable OFS
866866

867867
## $OutputEncoding
868868

869-
Determines the character encoding method that PowerShell uses when it sends
870-
text to other applications.
869+
Determines the character encoding method that PowerShell uses when piping data
870+
into native applications.
871871

872-
For example, if an application returns Unicode strings to PowerShell, you might
873-
need to change the value to **UnicodeEncoding** to send the characters
874-
correctly.
872+
> [!NOTE]
873+
> In the majority of scenarios, the value for `$OutputEncoding` should align
874+
> to the value of `[Console]::InputEncoding`.
875875
876876
The valid values are as follows: Objects derived from an Encoding class, such
877877
as [**ASCIIEncoding**][59], [**UTF7Encoding**][62], [**UTF8Encoding**][63],
@@ -881,51 +881,59 @@ as [**ASCIIEncoding**][59], [**UTF7Encoding**][62], [**UTF8Encoding**][63],
881881

882882
### Examples
883883

884-
This example shows how to make the Windows `findstr.exe` command work in
885-
PowerShell on a computer that's localized for a language that uses Unicode
886-
characters, such as Chinese.
887-
888884
The first command finds the value of `$OutputEncoding`. Because the value is an
889885
encoding object, display only its **EncodingName** property.
890886

891887
```powershell
892888
$OutputEncoding.EncodingName
893889
```
894890

895-
In this example, a `findstr.exe` command is used to search for two Chinese
896-
characters that are present in the `Test.txt` file. When this `findstr.exe`
897-
command is run in the Windows Command Prompt (`cmd.exe`), `findstr.exe` finds
898-
the characters in the text file. However, when you run the same `findstr.exe`
899-
command in PowerShell, the characters aren't found because the PowerShell sends
900-
them to `findstr.exe` in ASCII text, instead of in Unicode text.
891+
The remaining examples use the following PowerShell script saved as
892+
`hexdump.ps1` to illustrate the behavior of `$OutputEncoding`.
901893

902894
```powershell
903-
findstr <Unicode-characters>
895+
$inputStream = [Console]::OpenStandardInput()
896+
try {
897+
$buffer = [byte[]]::new(1024)
898+
$read = $inputStream.Read($buffer, 0, $buffer.Length)
899+
Format-Hex -InputObject $buffer -Count $read
900+
} finally {
901+
$inputStream.Dispose()
902+
}
904903
```
905904

906-
To make the command work in PowerShell, set the value of `$OutputEncoding` to
907-
the value of the **OutputEncoding** property of the console, that's based on
908-
the locale selected for Windows. Because **OutputEncoding** is a static
909-
property of the console, use double-colons (`::`) in the command.
905+
The following example shows how the string value `café` is encoded to bytes
906+
when piped into `hexdump.ps1` created above. It demonstrates that the string
907+
value is encoded using the [**UTF8Encoding**][63] scheme.
910908

911909
```powershell
912-
$OutputEncoding = [console]::OutputEncoding
913-
$OutputEncoding.EncodingName
910+
'café' | pwsh -File ./hexdump.ps1
914911
```
915912

916913
```Output
917-
OEM United States
914+
Label: Byte[] (System.Byte[]) <28873E25>
915+
916+
Offset Bytes Ascii
917+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
918+
------ ----------------------------------------------- -----
919+
0000000000000000 63 61 66 C3 A9 0D 0A caf�
918920
```
919921

920-
After the encoding change, the `findstr.exe` command finds the Unicode
921-
characters.
922+
The following example shows how the bytes change when changing the encoding
923+
to [**UnicodeEncoding**][60].
922924

923925
```powershell
924-
findstr <Unicode-characters>
926+
$OutputEncoding = [System.Text.Encoding]::Unicode
927+
'café' | pwsh -File ./hexdump.ps1
925928
```
926929

927930
```Output
928-
test.txt: <Unicode-characters>
931+
Label: Byte[] (System.Byte[]) <515A7DC3>
932+
933+
Offset Bytes Ascii
934+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
935+
------ ----------------------------------------------- -----
936+
0000000000000000 FF FE 63 00 61 00 66 00 E9 00 0D 00 0A 00 ÿþc a f é � �
929937
```
930938

931939
## $ProgressPreference

reference/7.3/Microsoft.PowerShell.Core/About/about_Preference_Variables.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -872,12 +872,12 @@ Remove-Variable OFS
872872

873873
## $OutputEncoding
874874

875-
Determines the character encoding method that PowerShell uses when it sends
876-
text to other applications.
875+
Determines the character encoding method that PowerShell uses when piping data
876+
into native applications.
877877

878-
For example, if an application returns Unicode strings to PowerShell, you might
879-
need to change the value to **UnicodeEncoding** to send the characters
880-
correctly.
878+
> [!NOTE]
879+
> In the majority of scenarios, the value for `$OutputEncoding` should align
880+
> to the value of `[Console]::InputEncoding`.
881881
882882
The valid values are as follows: Objects derived from an Encoding class, such
883883
as [**ASCIIEncoding**][62], [**UTF7Encoding**][65], [**UTF8Encoding**][66],
@@ -887,51 +887,59 @@ as [**ASCIIEncoding**][62], [**UTF7Encoding**][65], [**UTF8Encoding**][66],
887887

888888
### Examples
889889

890-
This example shows how to make the Windows `findstr.exe` command work in
891-
PowerShell on a computer that's localized for a language that uses Unicode
892-
characters, such as Chinese.
893-
894890
The first command finds the value of `$OutputEncoding`. Because the value is an
895891
encoding object, display only its **EncodingName** property.
896892

897893
```powershell
898894
$OutputEncoding.EncodingName
899895
```
900896

901-
In this example, a `findstr.exe` command is used to search for two Chinese
902-
characters that are present in the `Test.txt` file. When this `findstr.exe`
903-
command is run in the Windows Command Prompt (`cmd.exe`), `findstr.exe` finds
904-
the characters in the text file. However, when you run the same `findstr.exe`
905-
command in PowerShell, the characters aren't found because the PowerShell sends
906-
them to `findstr.exe` in ASCII text, instead of in Unicode text.
897+
The remaining examples use the following PowerShell script saved as
898+
`hexdump.ps1` to illustrate the behavior of `$OutputEncoding`.
907899

908900
```powershell
909-
findstr <Unicode-characters>
901+
$inputStream = [Console]::OpenStandardInput()
902+
try {
903+
$buffer = [byte[]]::new(1024)
904+
$read = $inputStream.Read($buffer, 0, $buffer.Length)
905+
Format-Hex -InputObject $buffer -Count $read
906+
} finally {
907+
$inputStream.Dispose()
908+
}
910909
```
911910

912-
To make the command work in PowerShell, set the value of `$OutputEncoding` to
913-
the value of the **OutputEncoding** property of the console, that's based on
914-
the locale selected for Windows. Because **OutputEncoding** is a static
915-
property of the console, use double-colons (`::`) in the command.
911+
The following example shows how the string value `café` is encoded to bytes
912+
when piped into `hexdump.ps1` created above. It demonstrates that the string
913+
value is encoded using the [**UTF8Encoding**][63] scheme.
916914

917915
```powershell
918-
$OutputEncoding = [console]::OutputEncoding
919-
$OutputEncoding.EncodingName
916+
'café' | pwsh -File ./hexdump.ps1
920917
```
921918

922919
```Output
923-
OEM United States
920+
Label: Byte[] (System.Byte[]) <28873E25>
921+
922+
Offset Bytes Ascii
923+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
924+
------ ----------------------------------------------- -----
925+
0000000000000000 63 61 66 C3 A9 0D 0A caf�
924926
```
925927

926-
After the encoding change, the `findstr.exe` command finds the Unicode
927-
characters.
928+
The following example shows how the bytes change when changing the encoding
929+
to [**UnicodeEncoding**][60].
928930

929931
```powershell
930-
findstr <Unicode-characters>
932+
$OutputEncoding = [System.Text.Encoding]::Unicode
933+
'café' | pwsh -File ./hexdump.ps1
931934
```
932935

933936
```Output
934-
test.txt: <Unicode-characters>
937+
Label: Byte[] (System.Byte[]) <515A7DC3>
938+
939+
Offset Bytes Ascii
940+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
941+
------ ----------------------------------------------- -----
942+
0000000000000000 FF FE 63 00 61 00 66 00 E9 00 0D 00 0A 00 ÿþc a f é � �
935943
```
936944

937945
## $ProgressPreference

reference/7.4/Microsoft.PowerShell.Core/About/about_Preference_Variables.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -872,12 +872,12 @@ Remove-Variable OFS
872872

873873
## $OutputEncoding
874874

875-
Determines the character encoding method that PowerShell uses when it sends
876-
text to other applications.
875+
Determines the character encoding method that PowerShell uses when piping data
876+
into native applications.
877877

878-
For example, if an application returns Unicode strings to PowerShell, you might
879-
need to change the value to **UnicodeEncoding** to send the characters
880-
correctly.
878+
> [!NOTE]
879+
> In the majority of scenarios, the value for `$OutputEncoding` should align
880+
> to the value of `[Console]::InputEncoding`.
881881
882882
The valid values are as follows: Objects derived from an Encoding class, such
883883
as [**ASCIIEncoding**][61], [**UTF7Encoding**][64], [**UTF8Encoding**][65],
@@ -887,51 +887,59 @@ as [**ASCIIEncoding**][61], [**UTF7Encoding**][64], [**UTF8Encoding**][65],
887887

888888
### Examples
889889

890-
This example shows how to make the Windows `findstr.exe` command work in
891-
PowerShell on a computer that's localized for a language that uses Unicode
892-
characters, such as Chinese.
893-
894890
The first command finds the value of `$OutputEncoding`. Because the value is an
895891
encoding object, display only its **EncodingName** property.
896892

897893
```powershell
898894
$OutputEncoding.EncodingName
899895
```
900896

901-
In this example, a `findstr.exe` command is used to search for two Chinese
902-
characters that are present in the `Test.txt` file. When this `findstr.exe`
903-
command is run in the Windows Command Prompt (`cmd.exe`), `findstr.exe` finds
904-
the characters in the text file. However, when you run the same `findstr.exe`
905-
command in PowerShell, the characters aren't found because the PowerShell sends
906-
them to `findstr.exe` in ASCII text, instead of in Unicode text.
897+
The remaining examples use the following PowerShell script saved as
898+
`hexdump.ps1` to illustrate the behavior of `$OutputEncoding`.
907899

908900
```powershell
909-
findstr <Unicode-characters>
901+
$inputStream = [Console]::OpenStandardInput()
902+
try {
903+
$buffer = [byte[]]::new(1024)
904+
$read = $inputStream.Read($buffer, 0, $buffer.Length)
905+
Format-Hex -InputObject $buffer -Count $read
906+
} finally {
907+
$inputStream.Dispose()
908+
}
910909
```
911910

912-
To make the command work in PowerShell, set the value of `$OutputEncoding` to
913-
the value of the **OutputEncoding** property of the console, that's based on
914-
the locale selected for Windows. Because **OutputEncoding** is a static
915-
property of the console, use double-colons (`::`) in the command.
911+
The following example shows how the string value `café` is encoded to bytes
912+
when piped into `hexdump.ps1` created above. It demonstrates that the string
913+
value is encoded using the [**UTF8Encoding**][63] scheme.
916914

917915
```powershell
918-
$OutputEncoding = [console]::OutputEncoding
919-
$OutputEncoding.EncodingName
916+
'café' | pwsh -File ./hexdump.ps1
920917
```
921918

922919
```Output
923-
OEM United States
920+
Label: Byte[] (System.Byte[]) <28873E25>
921+
922+
Offset Bytes Ascii
923+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
924+
------ ----------------------------------------------- -----
925+
0000000000000000 63 61 66 C3 A9 0D 0A caf�
924926
```
925927

926-
After the encoding change, the `findstr.exe` command finds the Unicode
927-
characters.
928+
The following example shows how the bytes change when changing the encoding
929+
to [**UnicodeEncoding**][60].
928930

929931
```powershell
930-
findstr <Unicode-characters>
932+
$OutputEncoding = [System.Text.Encoding]::Unicode
933+
'café' | pwsh -File ./hexdump.ps1
931934
```
932935

933936
```Output
934-
test.txt: <Unicode-characters>
937+
Label: Byte[] (System.Byte[]) <515A7DC3>
938+
939+
Offset Bytes Ascii
940+
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
941+
------ ----------------------------------------------- -----
942+
0000000000000000 FF FE 63 00 61 00 66 00 E9 00 0D 00 0A 00 ÿþc a f é � �
935943
```
936944

937945
## $ProgressPreference

0 commit comments

Comments
 (0)