You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clarify intended BOM convention for PowerShell files #2663
I noticed a difference between the documented encoding convention and what a few of the test-writing tools produce. I may be missing an intentional distinction here, so I'd appreciate clarification on the expected approach.
The contributing guide says to save all .ps1 and .psm1 files as UTF-8 with BOM. The VS Code settings also select utf8bom for PowerShell.
These examples were checked against upstream main at 506064a0147a54085f01d00797c169820a709a60.
Local check
On Linux / PowerShell 7.6.6 / PSScriptAnalyzer 1.25.0, I checked New-MtTest using the same source file as upstream. It produced a test script without BOM for both an ASCII title and a title containing an accented character. The PSUseBOMForUnicodeEncodedFile rule reported no warning for the ASCII case and one warning for the accented title.
That seems to explain why a file can pass that rule while still differing from the documented convention. I haven't encountered a runtime failure from this; the uncertainty is which convention contributors should follow.
Clarification requested
Is BOM intended for all PowerShell source and generated .ps1 / .psm1 files, or are some of these differences intentional?
Once the intended approach is clear, it would help to align the guidance and relevant tooling with it, or document any exceptions.
I measured the repo rather than reasoning about it, and the answer reframes the question: the three generators you found are not the gap. Nothing checks.
All counts are against main @ 6165598, over the 1,671 .ps1, .psm1 and .psd1 files in the tree.
What the repo actually does
Area
with BOM
without
of those, containing non-ASCII
tests/maester (shipped tests)
261
6
2
tests/ (ad, cis, cisa, orca, eidsca, asd)
512
120
0
powershell/ (module)
191
248
0
powershell/internal/checks
27
1
0
powershell/tests (unit tests)
103
131
5
build/
11
60
1
total
1,105
566
8
So 66.1% carry a BOM. The convention holds almost perfectly where the shipped tests live (261 of 267) and mostly does not hold anywhere else.
Why CI is green anyway
PSUseBOMForUnicodeEncodedFile is not excluded — powershell/tests/general/PSScriptAnalyzer.Tests.ps1 excludes only PSAvoidTrailingWhitespace and PSShouldProcess, and powershell/tests/pester.ps1:264 even prints a fix script when that rule fires. The rule works: I ran it directly on the eight BOM-less files that contain non-ASCII and all eight raise it as a Warning (positive control: a file with a BOM, powershell/public/run/Invoke-Maester.ps1, raises nothing).
The reason none of them ever fails a build is the analyzed path set. PSScriptAnalyzer.Tests.ps1:6 is:
Two of those are the family tests MT.1024 and MT.1059, which ship to users.
The part that decides what the convention can be
You already noticed it for the ASCII case, and it generalises: PSUseBOMForUnicodeEncodedFile only fires on files that contain non-ASCII. Of the 566 BOM-less files, it can ever see 8. So the rule is not a mechanism for "all .ps1 files carry a BOM" — it is a mechanism for "a file that needs a BOM has one", which is the narrower claim that matters for Windows PowerShell 5.1 reading accented characters.
That means the documented convention and the available enforcement are two different rules, which is, I think, the real source of the confusion you hit.
Two ways out, and they are different decisions
Keep the convention as written (every .ps1 and .psm1 is UTF-8 with BOM). Then PSScriptAnalyzer cannot enforce it and a separate check is needed: a three-byte prefix test over the tree, which is a handful of lines in a Pester test. It would currently fail on 566 files, so it needs a one-off sweep first — and the three generators (New-MtTest, the EIDSCA and ORCA updaters) have to change, or they reintroduce violations on every run.
Narrow the convention to what the tooling can enforce — a BOM is required where the file contains non-ASCII — and say so in contributing.md. Then the work is eight files plus making the generators emit a BOM only when they write non-ASCII, and PSUseBOMForUnicodeEncodedFile becomes the real gate once the analyzed path set covers tests/ and powershell/tests/.
Either way, widening $CommandPath is the change with the most value for the least risk, because today a rule that exists and works is pointed at a quarter of the repo.
Happy to send whichever you pick: the eight-file fix plus the path-set widening is small and safe; the repo-wide sweep plus a prefix test is bigger and touches 566 files, so I would not start it without @merill saying that is the convention he wants.
Measured on Linux, PowerShell 7 and PSScriptAnalyzer 1.25.0, reading the committed blobs rather than the working tree. What I did not check: whether any of the 566 would break under Windows PowerShell 5.1 in practice — for the 558 pure-ASCII ones there is nothing to break, and the 8 are the ones at risk.
Thanks for the extra analysis! Using BOMs where needed is essential; having clearly understandable and consistent standards is also very important. We added a .vscode config to the project that always uses utf8 with BOM for PowerShell files, but more and more files are now being generated outside of VS Code.
Are you aware of any downsides to simply always using the BOM, even when it's not "needed?"
I would lean towards a preference of always checking for it, but am not sure if it's worth the disruption of a failing CI check when the file itself might not truly depend on the marker.
Thanks @thetechgy for raising this, and for checking it against main with a reproduction instead of just a hunch. And thanks @earbona23 for measuring the whole tree. The table and the finding that PSScriptAnalyzer only ever looked at a quarter of the repo turned a style question into something we could actually decide. @SamErde, thanks for keeping it moving.
The ruling: every .ps1, .psm1 and .psd1 file is UTF-8 with a BOM, and Markdown and JSON have none.
One point that shaped this: Maester 3 runs only on PowerShell 7.4+ (CompatiblePSEditions = 'Core'), and PowerShell 7 reads BOM-less files as UTF-8. So the classic Windows PowerShell 5.1 reason for the BOM no longer applies at runtime. That's why nobody hit a failure, and why the eight BOM-less files with emoji work today. Also, the build already writes the merged Maester.psm1 with a BOM, so the module files never shipped without one. Only tests/ and packs/ are copied as-is.
So this is a consistency rule, not a bug fix. We kept the BOM rather than dropping it because two-thirds of the tree, the VS Code settings, the contributing guide and PSScriptAnalyzer's rule all already assume it. Switching the other way would mean more churn, and the BOM still helps anyone opening a test in Windows PowerShell ISE or an older editor.
On @SamErde's question about downsides: for these file types there aren't many. The one real one is that a BOM before a #!/usr/bin/env pwsh shebang breaks direct execution on Linux and macOS, and no tracked PowerShell file has a shebang. The cost of the CI check is kept low because the failure message prints the one-line fix.
As @earbona23 pointed out, PSScriptAnalyzer can't enforce "every file", so #2714 enforces it with a byte check instead:
New-MtTest, Convert-MtTest, New-MtPack, and the EIDSCA and ORCA generators now write PowerShell files with a BOM (Markdown without), so they stop reintroducing the problem.
A new .editorconfig sets utf-8-bom for PowerShell files, for editors and agents that don't use our VS Code settings.
A one-off sweep adds the BOM to the 573 files that were missing it. It only adds the three bytes, with no other content changes.
powershell/tests/general/FileEncoding.Tests.ps1 checks every tracked PowerShell file, and checks the scaffolding commands and generators.
contributing.md now covers .psd1, explains why, and fixes the suggested one-liner, which used Out-File and added an extra trailing newline.
@earbona23, thanks for offering to send this. It touched a lot of files at once, so I put it together in one go to keep conflicts with open PRs short. Your other suggestion, widening the PSScriptAnalyzer path set to cover tests/ and powershell/tests/, is still open and would be very welcome as a follow-up PR. It will probably surface some new warnings from other rules, so it's better as its own change.
If you have an open PR, merging main after #2714 lands should be clean unless you edited the first line of an existing file. New .ps1 files need the BOM, and the test will tell you if one is missing.
Thank you for the ruling, and for taking the sweep yourself. Doing it as one commit rather than letting it trickle in through open pull requests was the right call, and the reasoning for keeping the BOM rather than dropping it reads better than the version I had in my head: PowerShell 7.4+ makes it a consistency rule, not a runtime fix, and saying that plainly is more useful than either side of the argument on its own.
An apology is owed on my side. Eight files across five of my open pull requests were written without a BOM, which is work your sweep should not have had to think about. They are fixed now, each one exactly EF BB BF prepended and nothing else:
One judgment call, in case you would rather I had done the opposite. Four PowerShell files my branches touch already exist on main without a BOM — powershell/tests/functions/Invoke-MtGraphRequestCache.Tests.ps1 in #2685 and #2687, and powershell/public/report/Get-MtSafeMarkdown.ps1 with its test in #2715. I deliberately left those alone so #2714 owns their first line and we do not collide on it, exactly as your note warns. None of my edits touch line 1 of any of them, so those merges should stay clean. If you would rather each PR carried its own BOM, say so and I will add them.
On the follow-up you left open: I am measuring what widening $CommandPath to tests/ and powershell/tests/ actually surfaces, by rule and by file count, before sending anything. You are right that it will bring other rules with it, and the useful version of that pull request is one where the size is known up front rather than discovered in CI — so I would rather hand you the inventory with it than hand you a number of warnings.
One small thing I can confirm from this end, since it touches the same area: powershell/tests/general/FileEncoding.Tests.ps1 catches what PSUseBOMForUnicodeEncodedFile structurally cannot. Of the BOM-less files I measured, only eight contained non-ASCII, so the analyzer rule could ever see those eight — a byte-prefix check is the only thing that can express "every file", and that distinction is the part of #2663 that took the longest to see.
Closing the loop on the follow-up you left open, since this issue is now closed and I said I would measure before sending: it is #2716.
The numbers, so they are here rather than only in the pull request:
analyzed set goes from 457 to 1,585.ps1 files;
widening surfaces 143 findings, all Warning, none Error, and 139 of them are in powershell/tests/;
those describe how a command should be written rather than a defect in test code, so they are excluded only under a test path — the module's own commands are still held to every one of them, with the reason written next to each;
Two of the 143 would have been wrong to "fix", which is the part I would not have known without measuring: PSUseSingularNouns wants shipped test functions such as Test-MtCisGuestAccessReviews renamed, and PSAvoidUsingPlainTextForPassword fires on $PasswordValidityPeriodInDays, which is a number of days.
It is rebased onto main after #2714 — it had collided on the first line of PSScriptAnalyzer.Tests.ps1, exactly the case your description warned about.
Activity
SamErde commentedon Nov 3, 2026
You are right to question the differences there. UTF-8 with BOM should still be the standard as far as I know.
earbona23 commentedon Nov 7, 2026
I measured the repo rather than reasoning about it, and the answer reframes the question: the three generators you found are not the gap. Nothing checks.
All counts are against
main@ 6165598, over the 1,671.ps1,.psm1and.psd1files in the tree.What the repo actually does
tests/maester(shipped tests)tests/(ad, cis, cisa, orca, eidsca, asd)powershell/(module)powershell/internal/checkspowershell/tests(unit tests)build/So 66.1% carry a BOM. The convention holds almost perfectly where the shipped tests live (261 of 267) and mostly does not hold anywhere else.
Why CI is green anyway
PSUseBOMForUnicodeEncodedFileis not excluded —powershell/tests/general/PSScriptAnalyzer.Tests.ps1excludes onlyPSAvoidTrailingWhitespaceandPSShouldProcess, andpowershell/tests/pester.ps1:264even prints a fix script when that rule fires. The rule works: I ran it directly on the eight BOM-less files that contain non-ASCII and all eight raise it as a Warning (positive control: a file with a BOM,powershell/public/run/Invoke-Maester.ps1, raises nothing).The reason none of them ever fails a build is the analyzed path set.
PSScriptAnalyzer.Tests.ps1:6is:That is 457 files, 27.3% of the tree. The other 1,214 are never analyzed — and all eight violations are outside it:
tests/maester/entra/Test.MT.1024.ps1tests/maester/defender/Test.MT.1059.ps1powershell/tests/functions/Get-MtAffectedObject.Tests.ps1powershell/tests/functions/Get-MtHtmlReport.Tests.ps1powershell/tests/functions/Get-MtUser.Tests.ps1powershell/tests/functions/Test-MtContext.Tests.ps1powershell/tests/functions/ZeroTrustAssessmentPack.Tests.ps1build/Update-MaesterConfigVersion.ps1Two of those are the family tests
MT.1024andMT.1059, which ship to users.The part that decides what the convention can be
You already noticed it for the ASCII case, and it generalises:
PSUseBOMForUnicodeEncodedFileonly fires on files that contain non-ASCII. Of the 566 BOM-less files, it can ever see 8. So the rule is not a mechanism for "all.ps1files carry a BOM" — it is a mechanism for "a file that needs a BOM has one", which is the narrower claim that matters for Windows PowerShell 5.1 reading accented characters.That means the documented convention and the available enforcement are two different rules, which is, I think, the real source of the confusion you hit.
Two ways out, and they are different decisions
.ps1and.psm1is UTF-8 with BOM). Then PSScriptAnalyzer cannot enforce it and a separate check is needed: a three-byte prefix test over the tree, which is a handful of lines in a Pester test. It would currently fail on 566 files, so it needs a one-off sweep first — and the three generators (New-MtTest, the EIDSCA and ORCA updaters) have to change, or they reintroduce violations on every run.contributing.md. Then the work is eight files plus making the generators emit a BOM only when they write non-ASCII, andPSUseBOMForUnicodeEncodedFilebecomes the real gate once the analyzed path set coverstests/andpowershell/tests/.Either way, widening
$CommandPathis the change with the most value for the least risk, because today a rule that exists and works is pointed at a quarter of the repo.Happy to send whichever you pick: the eight-file fix plus the path-set widening is small and safe; the repo-wide sweep plus a prefix test is bigger and touches 566 files, so I would not start it without @merill saying that is the convention he wants.
Measured on Linux, PowerShell 7 and PSScriptAnalyzer 1.25.0, reading the committed blobs rather than the working tree. What I did not check: whether any of the 566 would break under Windows PowerShell 5.1 in practice — for the 558 pure-ASCII ones there is nothing to break, and the 8 are the ones at risk.
SamErde commentedon Nov 7, 2026
Thanks for the extra analysis! Using BOMs where needed is essential; having clearly understandable and consistent standards is also very important. We added a
.vscodeconfig to the project that always usesutf8 with BOMfor PowerShell files, but more and more files are now being generated outside of VS Code.Are you aware of any downsides to simply always using the BOM, even when it's not "needed?"
I would lean towards a preference of always checking for it, but am not sure if it's worth the disruption of a failing CI check when the file itself might not truly depend on the marker.
What are your thoughts?
merill commentedon Nov 7, 2026
Thanks @thetechgy for raising this, and for checking it against
mainwith a reproduction instead of just a hunch. And thanks @earbona23 for measuring the whole tree. The table and the finding that PSScriptAnalyzer only ever looked at a quarter of the repo turned a style question into something we could actually decide. @SamErde, thanks for keeping it moving.The ruling: every
.ps1,.psm1and.psd1file is UTF-8 with a BOM, and Markdown and JSON have none.One point that shaped this: Maester 3 runs only on PowerShell 7.4+ (
CompatiblePSEditions = 'Core'), and PowerShell 7 reads BOM-less files as UTF-8. So the classic Windows PowerShell 5.1 reason for the BOM no longer applies at runtime. That's why nobody hit a failure, and why the eight BOM-less files with emoji work today. Also, the build already writes the mergedMaester.psm1with a BOM, so the module files never shipped without one. Onlytests/andpacks/are copied as-is.So this is a consistency rule, not a bug fix. We kept the BOM rather than dropping it because two-thirds of the tree, the VS Code settings, the contributing guide and PSScriptAnalyzer's rule all already assume it. Switching the other way would mean more churn, and the BOM still helps anyone opening a test in Windows PowerShell ISE or an older editor.
On @SamErde's question about downsides: for these file types there aren't many. The one real one is that a BOM before a
#!/usr/bin/env pwshshebang breaks direct execution on Linux and macOS, and no tracked PowerShell file has a shebang. The cost of the CI check is kept low because the failure message prints the one-line fix.As @earbona23 pointed out, PSScriptAnalyzer can't enforce "every file", so #2714 enforces it with a byte check instead:
New-MtTest,Convert-MtTest,New-MtPack, and the EIDSCA and ORCA generators now write PowerShell files with a BOM (Markdown without), so they stop reintroducing the problem..editorconfigsetsutf-8-bomfor PowerShell files, for editors and agents that don't use our VS Code settings.powershell/tests/general/FileEncoding.Tests.ps1checks every tracked PowerShell file, and checks the scaffolding commands and generators.contributing.mdnow covers.psd1, explains why, and fixes the suggested one-liner, which usedOut-Fileand added an extra trailing newline.@earbona23, thanks for offering to send this. It touched a lot of files at once, so I put it together in one go to keep conflicts with open PRs short. Your other suggestion, widening the PSScriptAnalyzer path set to cover
tests/andpowershell/tests/, is still open and would be very welcome as a follow-up PR. It will probably surface some new warnings from other rules, so it's better as its own change.If you have an open PR, merging
mainafter #2714 lands should be clean unless you edited the first line of an existing file. New.ps1files need the BOM, and the test will tell you if one is missing.fix: keep Markdown, including test guides, UTF-8 without BOM
chore: remove the UTF-8 BOM from Markdown files
chore: save the new PowerShell files as UTF-8 with BOM
chore: save the new PowerShell files as UTF-8 with BOM
chore: save the new PowerShell files as UTF-8 with BOM
chore: save the new PowerShell files as UTF-8 with BOM
chore: save the new smoke test as UTF-8 with BOM
earbona23 commentedon Nov 7, 2026
Thank you for the ruling, and for taking the sweep yourself. Doing it as one commit rather than letting it trickle in through open pull requests was the right call, and the reasoning for keeping the BOM rather than dropping it reads better than the version I had in my head: PowerShell 7.4+ makes it a consistency rule, not a runtime fix, and saying that plainly is more useful than either side of the argument on its own.
An apology is owed on my side. Eight files across five of my open pull requests were written without a BOM, which is work your sweep should not have had to think about. They are fixed now, each one exactly
EF BB BFprepended and nothing else:tests/maester/azure/Test.MT.1266.ps1Get-MtDefenderForCloudRecommendation.ps1,Test-MtAzureDefenderForCloud.Tests.ps1,Test.MT.1257.ps1Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1Get-MtDefenderExploitableVulnerability.ps1,Test.MT.1256.ps1powershell/tests/general/SmokeTestPermissions.Tests.ps1One judgment call, in case you would rather I had done the opposite. Four PowerShell files my branches touch already exist on
mainwithout a BOM —powershell/tests/functions/Invoke-MtGraphRequestCache.Tests.ps1in #2685 and #2687, andpowershell/public/report/Get-MtSafeMarkdown.ps1with its test in #2715. I deliberately left those alone so #2714 owns their first line and we do not collide on it, exactly as your note warns. None of my edits touch line 1 of any of them, so those merges should stay clean. If you would rather each PR carried its own BOM, say so and I will add them.On the follow-up you left open: I am measuring what widening
$CommandPathtotests/andpowershell/tests/actually surfaces, by rule and by file count, before sending anything. You are right that it will bring other rules with it, and the useful version of that pull request is one where the size is known up front rather than discovered in CI — so I would rather hand you the inventory with it than hand you a number of warnings.One small thing I can confirm from this end, since it touches the same area:
powershell/tests/general/FileEncoding.Tests.ps1catches whatPSUseBOMForUnicodeEncodedFilestructurally cannot. Of the BOM-less files I measured, only eight contained non-ASCII, so the analyzer rule could ever see those eight — a byte-prefix check is the only thing that can express "every file", and that distinction is the part of #2663 that took the longest to see.earbona23 commentedon Nov 7, 2026
Closing the loop on the follow-up you left open, since this issue is now closed and I said I would measure before sending: it is #2716.
The numbers, so they are here rather than only in the pull request:
.ps1files;Warning, noneError, and 139 of them are inpowershell/tests/;PSUseBOMForUnicodeEncodedFilefindings that were left went with your sweep.Two of the 143 would have been wrong to "fix", which is the part I would not have known without measuring:
PSUseSingularNounswants shipped test functions such asTest-MtCisGuestAccessReviewsrenamed, andPSAvoidUsingPlainTextForPasswordfires on$PasswordValidityPeriodInDays, which is a number of days.It is rebased onto
mainafter #2714 — it had collided on the first line ofPSScriptAnalyzer.Tests.ps1, exactly the case your description warned about.fix(ci): analyze the tests with PSScriptAnalyzer, not just the comman…
fix(ci): document every default Graph scope the smoke test consents (#…
fix(MT.1087): no pass when the hunting query returned nothing at all (#…