Skip to content

Commit f2fcf42

Browse files
authored
Provide more guidance for file encoding (#285)
* Provide more guidance for file encoding * Update HelpFile encoding rule
1 parent 56235ca commit f2fcf42

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

reference/docs-conceptual/PSScriptAnalyzer/Rules/UseBOMForUnicodeEncodedFile.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Use BOM encoding for non-ASCII files
3-
ms.date: 06/28/2023
3+
ms.date: 01/07/2025
44
ms.topic: reference
55
title: UseBOMForUnicodeEncodedFile
66
---
@@ -13,6 +13,30 @@ title: UseBOMForUnicodeEncodedFile
1313
For a file encoded with a format other than ASCII, ensure Byte Order Mark (BOM) is present to ensure
1414
that any application consuming this file can interpret it correctly.
1515

16+
You can use this rule to test any arbitrary text file, but the intent is to ensure that PowerShell
17+
scripts are saved with a BOM when using a Unicode encoding.
18+
1619
## How
1720

18-
Ensure that the file is encoded with BOM present.
21+
For PowerShell commands that write to files, ensure that you set the encoding parameter to a value
22+
that produces a BOM. In PowerShell 7 and higher, the following values of the **Encoding** parameter
23+
produce a BOM:
24+
25+
- `bigendianunicode`
26+
- `bigendianutf32`
27+
- `oem`
28+
- `unicode`
29+
- `utf32`
30+
- `utf8BOM`
31+
32+
When you create a script file using a text editor, ensure that the editor is configured to save the
33+
file with a BOM. Consult the documentation for your text editor for instructions on how to save
34+
files with a BOM.
35+
36+
## Further reading
37+
38+
For more information, see the following articles:
39+
40+
- [about_Character_Encoding](/powershell/module/microsoft.powershell.core/about/about_character_encoding)
41+
- [Set-Content](xref:Microsoft.PowerShell.Management.Set-Content)
42+
- [Understanding file encoding in VS Code and PowerShell](/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding)
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Use UTF8 Encoding For Help File
3-
ms.date: 06/28/2023
3+
ms.date: 01/07/2025
44
ms.topic: reference
55
title: UseUTF8EncodingForHelpFile
66
---
@@ -10,4 +10,24 @@ title: UseUTF8EncodingForHelpFile
1010

1111
## Description
1212

13-
Check if help file uses UTF-8 encoding.
13+
Check that an `about_` help file uses UTF-8 encoding. The filename must start with `about_` and end
14+
with `.help.txt`. The rule uses the **CurrentEncoding** property of the **StreamReader** class to
15+
determine the encoding of the file.
16+
17+
## How
18+
19+
For PowerShell commands that write to files, ensure that you set the encoding parameter to `utf8`,
20+
`utf8BOM`, or `utf8NoBOM`.
21+
22+
When you create a help file using a text editor, ensure that the editor is configured to save the
23+
file in a UTF8 format. Consult the documentation for your text editor for instructions on how to
24+
save files with a specific encoding.
25+
26+
## Further reading
27+
28+
For more information, see the following articles:
29+
30+
- [System.IO.StreamReader](xref:System.IO.StreamReader.CurrentEncoding%2A)
31+
- [about_Character_Encoding](/powershell/module/microsoft.powershell.core/about/about_character_encoding)
32+
- [Set-Content](xref:Microsoft.PowerShell.Management.Set-Content)
33+
- [Understanding file encoding in VS Code and PowerShell](/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding)

0 commit comments

Comments
 (0)