PowerShell Get-Help注释生成帮助文档空行过多问题求助
PowerShell Get-Help 空行问题及优化方案
问题说明
无法理解PowerShell中Get-Help输出空行的规律,有时会出现1行、2行甚至3行空行。尝试过添加换行、移除制表符,但仍存在奇怪的空行,想确认操作是否有误,以及如何优化输出格式使其更美观。
原函数注释代码
Function New-ChromeDriver { <# .SYNOPSIS Start the ChromeDriver and launch a new Chrome Browser for powershell automation. .DESCRIPTION This function create a new Chrome with the ChromeDriver with all the required options. You can use the flags to start it incognito and/or headless. Really usefull for powershell automation. Will create and populate global variables : $Driver, $Wait, $mainHandle and $ExpectedConditions .INPUTS -Incognito [Optional flag] -Headless [Optional flag] .OUTPUTS None .LINK Confluence to be added .LINK GitLab to be added #> [CmdletBinding()] [OutputType([System.Void])] Param ( # If present, activate the incognito browsing mode [parameter(mandatory=$false)][switch]$incognito, # If present, Chrome will start headless (not shown at all in windows) [parameter(mandatory=$false)][switch]$headless ) Try { ... } Catch { ... } }
原Get-Help输出结果
PS C:\> Get-Help New-ChromeDriver -full NAME New-ChromeDriver SYNOPSIS Start the ChromeDriver and launch a new Chrome Browser for powershell automation. SYNTAX New-ChromeDriver [-incognito] [-headless] [<CommonParameters>] DESCRIPTION This function create a new Chrome with the ChromeDriver with all the required options. You can use the flags to start it incognito and/or headless. Really usefull for powershell automation. Will create and populate global variables : $Driver, $Wait, $mainHandle and $ExpectedConditions PARAMETERS -incognito [<SwitchParameter>] If present, activate the incognito browsing mode Required? false Position? named Default value False Accept pipeline input? false Accept wildcard characters? false -headless [<SwitchParameter>] If present, Chrome will start headless (not shown at all in windows) Required? false Position? named Default value False Accept pipeline input? false Accept wildcard characters? false <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). INPUTS -Incognito [Optional flag] -Headless [Optional flag] OUTPUTS None RELATED LINKS Confluence to be added GitLab to be added
原因分析与优化方案
空行产生的核心原因
- 注释块空行的解析叠加:PowerShell帮助解析器会保留注释块内的空行,同时不同帮助节(如SYNOPSIS、SYNTAX)之间还有默认间距,两者叠加就会出现多层空行。
- .INPUTS节的错误用法:原注释里把参数列表写在了.INPUTS节,这不符合规范——.INPUTS节本该描述管道输入类型,而非罗列参数,这种错误写法会导致解析器额外生成空行。
- 参数注释的格式冗余:参数上方的注释与参数定义之间的空行,会被解析器放大为输出中的多行空行。
具体优化步骤
规范帮助节内容格式
- 每个帮助标签(如
.SYNOPSIS)与内容直接衔接,不要留空行。 - 同一节内的段落之间只留1个空行,避免连续多个空行。
- 修正.INPUTS节:如果函数不接受管道输入,直接写
None,不要在这里列参数。
调整后的注释块示例:
<# .SYNOPSIS Start the ChromeDriver and launch a new Chrome Browser for PowerShell automation. .DESCRIPTION This function creates a new Chrome instance with ChromeDriver and all required options. You can use flags to start it in incognito and/or headless mode, which is very useful for PowerShell automation. It creates and populates global variables: $Driver, $Wait, $mainHandle and $ExpectedConditions .INPUTS None .OUTPUTS None .LINK Confluence to be added .LINK GitLab to be added #>- 每个帮助标签(如
精简参数注释格式
- 参数注释直接紧跟参数定义,不要留空行,保持紧凑:
Param ( # Activate incognito browsing mode [parameter(Mandatory=$false)][switch]$incognito, # Start Chrome in headless mode (no visible window) [parameter(Mandatory=$false)][switch]$headless )
- 参数注释直接紧跟参数定义,不要留空行,保持紧凑:
可选:用platyPS模块标准化帮助
如果你需要更严格的格式控制,可以安装platyPS模块,它能生成标准Markdown格式的帮助文档,再导入到PowerShell中,让Get-Help的输出完全符合预期,彻底避免空行混乱问题。
内容的提问来源于stack exchange,提问作者Sire Hugolin
相关产品推荐
相关产品推荐

