Project

PSPublishModule

Build, test, version, sign, and publish PowerShell modules with reusable PowerForge pipelines.

Stars 48
Forks 16
PowerShell Gallery downloads 17,213
Release v3.0.169
Language: C# Updated: 2026-10-17
Command

Remove-Comments

Module PSPublishModule
Inputs
None

Removes PowerShell comments from a script file or provided content, with optional empty-line normalization.

Syntax

Remove-Comments [-DestinationFilePath <String>] [-DoNotRemoveSignatureBlock] [-RemoveAllEmptyLines] [-RemoveCommentsBeforeParamBlock] [-RemoveCommentsInParamBlock] [-RemoveEmptyLines] -SourceFilePath <String> [<CommonParameters>]
#
Parameter set: FilePath
Remove-Comments -Content <String> [-DestinationFilePath <String>] [-DoNotRemoveSignatureBlock] [-RemoveAllEmptyLines] [-RemoveCommentsBeforeParamBlock] [-RemoveCommentsInParamBlock] [-RemoveEmptyLines] [<CommonParameters>]
#
Parameter set: Content

Description

Uses the PowerShell parser (AST) to remove comments safely rather than relying on fragile regex-only approaches. Useful as a preprocessing step when producing merged/packed scripts.

Examples

Authored help example

Remove comments from a file and write to a new file

Remove-Comments -SourceFilePath '.\Public\Get-Thing.ps1' -DestinationFilePath '.\Public\Get-Thing.nocomments.ps1'

Writes the cleaned content to the destination file.

Remove comments from content and return the processed text

$clean = Remove-Comments -Content (Get-Content -Raw .\script.ps1)

Returns the processed content when no destination file is specified.

Parameters

-DestinationFilePath String optionalaliases: Destination, OutputFile, OutputFilePath
File path to the destination file. If not provided, the content is returned.
-DoNotRemoveSignatureBlock SwitchParameter optional
Do not remove a signature block, if present.
-RemoveAllEmptyLines SwitchParameter optional
Remove all empty lines from the content.
-RemoveCommentsBeforeParamBlock SwitchParameter optional
Remove comments before the param block. By default comments before the param block are not removed.
-RemoveCommentsInParamBlock SwitchParameter optional
Remove comments in the param block. By default comments in the param block are not removed.
-RemoveEmptyLines SwitchParameter optional
Remove empty lines if more than one empty line is found.
-SourceFilePath String requiredaliases: FilePath, LiteralPath, Pathsets: FilePath
File path to the source file.
-Content String requiredsets: Content
Raw file content to process.

Common Parameters

This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.

For more information, see about_CommonParameters.