PowerShell's comment-based help system for script-based functions supports the .EXAMPLE directive to document usage examples. When Get-Help -Examples is invoked, each example is displayed with an auto-generated heading like EXAMPLE 1, EXAMPLE 2, etc.
Compiled cmdlets that use MAML-based help already support custom titles on examples (e.g., EXAMPLE 1: Retrieving an item). However, comment-based help — the primary documentation method for script-based functions and modules — has no equivalent mechanism.
This gap also affects documentation tooling. PlatyPS cannot generate titled examples from comment-based help because the parser does not expose title data. This was reported as PlatyPS issue #627, but the root cause is in PowerShell's parser itself.
Related: #23814 — Get-Help shows incorrect spacing and unwanted prefixes for first line of .EXAMPLE text.
Request
Support an optional title on the .EXAMPLE directive in comment-based help, following the same inline pattern used by .PARAMETER <name>:
Current behavior
Placing text on the same line as .EXAMPLE causes the parser to fall through to an unhandled default: case in the directive switch, which returns false and breaks help parsing entirely. The example — and potentially the entire help block — is silently discarded.
function Show-Example {
<#
.EXAMPLE Retrieving an item from a directory
Get-Item -Path C:\Temp
Retrieves the item at C:\Temp
#>
param()
}
Get-Help Show-Example -Examples produces no output because the parser rejects the help block.
Expected behavior
The parser should extract the title and display it alongside the auto-generated number:
-------------------------- EXAMPLE 1: Retrieving an item from a directory --------------------------
Get-Item -Path C:\Temp
Retrieves the item at C:\Temp
Examples without titles should continue to display exactly as they do today — no behavioral change for existing help content.
Acceptance criteria
.EXAMPLE <Title> syntax is supported, with the title extracted and stored separately from the example body
Get-Help -Examples displays titles as EXAMPLE N: <Title> when a title is present
- Examples without titles display identically to current behavior (
EXAMPLE N with no colon or trailing text)
- Existing help content is not affected — full backward compatibility (no breaking API changes)
- The
CommentHelpInfo public API exposes example titles so external tools (e.g., PlatyPS) can consume them
- Round-tripping via
GetCommentBlock() preserves titles
- Round-tripping via
ProxyCommand.GetHelpComments() preserves titles
Technical decisions
Parser handling — dual switch blocks. The HelpCommentsParser.AnalyzeCommentBlock method uses a directive regex ^\s*\.(\w+)(\s+(\S.*))?\s*$ that routes to two separate switch blocks: one when Groups[3].Success is true (text follows the keyword), and one when it is false. Currently EXAMPLE is only handled in the second block. To support .EXAMPLE <Title>, add a case "EXAMPLE" to the first block that captures match.Groups[3].Value.Trim() as the title, then falls through to collecting the body via GetSection(). A new List<string> _exampleTitles field stores titles in order, with an empty string inserted when no title is provided.
New public property on CommentHelpInfo (non-breaking). Add ReadOnlyCollection<string> ExampleTitles { get; internal set; } to the CommentHelpInfo class as a parallel collection alongside the existing Examples property. Each index in ExampleTitles corresponds to the same index in Examples. An empty string entry means no title for that example. The existing Examples property type (ReadOnlyCollection<string>) is preserved unchanged so the public API remains binary- and source-compatible.
Title format in XML generation. In the XML-building section of HelpCommentsParser (around line 363), when a title is present the example heading is built as EXAMPLE N: <Title>; when no title is present, the existing EXAMPLE N format is emitted unchanged. This matches the format used by MAML-based help for compiled cmdlets.
GetCommentBlock() round-tripping. Update the GetCommentBlock() method in CommentHelpInfo to emit .EXAMPLE <title> when ExampleTitles[index] is non-empty, and .EXAMPLE on its own line when the title is empty — preserving round-trip fidelity in both directions.
ProxyCommand.GetHelpComments round-tripping. Update ProxyCommand.GetHelpComments to emit .EXAMPLE <title> when the underlying MAML title contains a user-provided portion. A new private helper ExtractExampleTitle recovers the original user title from the decorated MAML title string. The extraction is culture-agnostic — it anchors on the dash-padding and the : separator rather than the literal English word EXAMPLE, so it works correctly under any UI culture.
GetExampleSections — no change needed. The static method that splits example content into prompt, code, and remarks operates on the body text, which is captured separately from the title. No modification required.
HelpParagraphBuilder — no change needed. The WPF help window already reads the title property from the XML example node. Since the title will be embedded in the XML by the parser, the UI will display it automatically.
Backward compatibility. No breaking changes. The syntax .EXAMPLE without trailing text continues to work identically. Only the new .EXAMPLE <Title> form adds behavior. The ExampleTitles property is purely additive to the public API; the existing Examples property type is unchanged.
Test strategy. Extend existing tests in ScriptHelp.Tests.ps1 and add coverage for ProxyCommand round-tripping in ProxyCommand.Tests.ps1. Cover: titled examples, untitled examples, mixed titled/untitled, line-comment syntax, parity between Examples and ExampleTitles, edge-case titles (containing colons, dashes, ending with a dash), and round-tripping via both GetCommentBlock() and ProxyCommand.GetHelpComments().
Implementation plan
Parser changes
Data model changes
XML generation changes
Round-trip serialization
Tests
Note: Only the reference/7.x folder matching the shipping PowerShell version needs updating. Older version folders (5.1, 7.2, 7.4, 7.5) should not be changed.
PlatyPS compatibility
VSCode PowerShell extension
Markdown / syntax highlighting grammars
PowerShell's comment-based help system for script-based functions supports the
.EXAMPLEdirective to document usage examples. WhenGet-Help -Examplesis invoked, each example is displayed with an auto-generated heading likeEXAMPLE 1,EXAMPLE 2, etc.Compiled cmdlets that use MAML-based help already support custom titles on examples (e.g.,
EXAMPLE 1: Retrieving an item). However, comment-based help — the primary documentation method for script-based functions and modules — has no equivalent mechanism.This gap also affects documentation tooling. PlatyPS cannot generate titled examples from comment-based help because the parser does not expose title data. This was reported as PlatyPS issue #627, but the root cause is in PowerShell's parser itself.
Related: #23814 —
Get-Helpshows incorrect spacing and unwanted prefixes for first line of.EXAMPLEtext.Request
Support an optional title on the
.EXAMPLEdirective in comment-based help, following the same inline pattern used by.PARAMETER <name>:Current behavior
Placing text on the same line as
.EXAMPLEcauses the parser to fall through to an unhandleddefault:case in the directive switch, which returnsfalseand breaks help parsing entirely. The example — and potentially the entire help block — is silently discarded.Get-Help Show-Example -Examplesproduces no output because the parser rejects the help block.Expected behavior
The parser should extract the title and display it alongside the auto-generated number:
Examples without titles should continue to display exactly as they do today — no behavioral change for existing help content.
Acceptance criteria
.EXAMPLE <Title>syntax is supported, with the title extracted and stored separately from the example bodyGet-Help -Examplesdisplays titles asEXAMPLE N: <Title>when a title is presentEXAMPLE Nwith no colon or trailing text)CommentHelpInfopublic API exposes example titles so external tools (e.g., PlatyPS) can consume themGetCommentBlock()preserves titlesProxyCommand.GetHelpComments()preserves titlesTechnical decisions
Parser handling — dual switch blocks. The HelpCommentsParser.AnalyzeCommentBlock method uses a directive regex
^\s*\.(\w+)(\s+(\S.*))?\s*$that routes to two separateswitchblocks: one whenGroups[3].Successistrue(text follows the keyword), and one when it isfalse. CurrentlyEXAMPLEis only handled in the second block. To support.EXAMPLE <Title>, add acase "EXAMPLE"to the first block that capturesmatch.Groups[3].Value.Trim()as the title, then falls through to collecting the body viaGetSection(). A newList<string> _exampleTitlesfield stores titles in order, with an empty string inserted when no title is provided.New public property on
CommentHelpInfo(non-breaking). AddReadOnlyCollection<string> ExampleTitles { get; internal set; }to the CommentHelpInfo class as a parallel collection alongside the existingExamplesproperty. Each index inExampleTitlescorresponds to the same index inExamples. An empty string entry means no title for that example. The existingExamplesproperty type (ReadOnlyCollection<string>) is preserved unchanged so the public API remains binary- and source-compatible.Title format in XML generation. In the XML-building section of
HelpCommentsParser(around line 363), when a title is present the example heading is built asEXAMPLE N: <Title>; when no title is present, the existingEXAMPLE Nformat is emitted unchanged. This matches the format used by MAML-based help for compiled cmdlets.GetCommentBlock()round-tripping. Update theGetCommentBlock()method inCommentHelpInfoto emit.EXAMPLE <title>whenExampleTitles[index]is non-empty, and.EXAMPLEon its own line when the title is empty — preserving round-trip fidelity in both directions.ProxyCommand.GetHelpCommentsround-tripping. UpdateProxyCommand.GetHelpCommentsto emit.EXAMPLE <title>when the underlying MAML title contains a user-provided portion. A new private helperExtractExampleTitlerecovers the original user title from the decorated MAML title string. The extraction is culture-agnostic — it anchors on the dash-padding and the:separator rather than the literal English wordEXAMPLE, so it works correctly under any UI culture.GetExampleSections— no change needed. The static method that splits example content into prompt, code, and remarks operates on the body text, which is captured separately from the title. No modification required.HelpParagraphBuilder— no change needed. The WPF help window already reads thetitleproperty from the XML example node. Since the title will be embedded in the XML by the parser, the UI will display it automatically.Backward compatibility. No breaking changes. The syntax
.EXAMPLEwithout trailing text continues to work identically. Only the new.EXAMPLE <Title>form adds behavior. TheExampleTitlesproperty is purely additive to the public API; the existingExamplesproperty type is unchanged.Test strategy. Extend existing tests in ScriptHelp.Tests.ps1 and add coverage for
ProxyCommandround-tripping in ProxyCommand.Tests.ps1. Cover: titled examples, untitled examples, mixed titled/untitled, line-comment syntax, parity betweenExamplesandExampleTitles, edge-case titles (containing colons, dashes, ending with a dash), and round-tripping via bothGetCommentBlock()andProxyCommand.GetHelpComments().Implementation plan
Parser changes
case "EXAMPLE"to the firstswitchblock inAnalyzeCommentBlock(theGroups[3].Successbranch) — extract title frommatch.Groups[3].Value.Trim(), append it to_exampleTitles, and callGetSection()to store the body in_examplesswitchblock (the no-arguments branch), append an empty string to_exampleTitlesfor the existingcase "EXAMPLE"to keep title indices aligned with body indicesList<string> _exampleTitlesfield toHelpCommentsParserthat runs in parallel with_examples_sections.ExampleTitlesasReadOnlyCollection<string>after parsing completesData model changes
ReadOnlyCollection<string> ExampleTitles { get; internal set; }property toCommentHelpInfo(parallel collection — non-breaking;Examplesproperty type is unchanged)XML generation changes
HelpCommentsParserto conditionally append: <title>when the example title is non-emptyRound-trip serialization
GetCommentBlock()inCommentHelpInfoto emit.EXAMPLE <title>when the title is non-empty, and.EXAMPLEon its own line when untitledProxyCommand.GetHelpCommentsto emit.EXAMPLE <title>when the MAML title contains a user-provided titleExtractExampleTitlehelper toProxyCommandthat recovers the user title from the decorated MAML title string (anchored on dashes/:separator, not the literal wordEXAMPLE)Tests
$help.examples.example.titlecontains the custom titleGetCommentBlock()round-tripping — verify titles survive serialization and deserialization.EXAMPLE <Title>no longer breaks help parsing (the previousdefault: return falsebug)ProxyCommand.GetHelpCommentsround-tripping titled examples# .EXAMPLE Title) titled examplesExamples.CountandExampleTitles.Countare always equalDocumentation (MicrosoftDocs/PowerShell-Docs)
.EXAMPLEkeyword section inabout_Comment_Based_Helpto document.EXAMPLE <Title>syntax — mirror how.PARAMETER <Name>is already described on the same pagereference/7.x/Microsoft.PowerShell.Core/About/about_Comment_Based_Help.md.EXAMPLE <Title>reference/docs-conceptual/developer/help/writing-comment-based-help-topics.mdreference/docs-conceptual/whats-new/What-s-New-in-PowerShell-7x.mdPlatyPS compatibility
CommentHelpInfo.ExampleTitlesproperty (PlatyPS issue #627)VSCode PowerShell extension
.EXAMPLE <Title>is recognized as a directive keyword, not body text — the.EXAMPLEkeyword should retain its styling regardless of trailing title text on the same lineMarkdown / syntax highlighting grammars
.EXAMPLE <Title>is treated the same way.PARAMETER <Name>is — the directive keyword should be recognized regardless of trailing text on the same line