API Reference
Remove-Comments
Removes PowerShell comments from a script file or provided content, with optional empty-line normalization.
Remarks
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
Remove comments from a file and write to a new file
PS>
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
PS>
$clean = Remove-Comments -Content (Get-Content -Raw .\script.ps1)
Returns the processed content when no destination file is specified.
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.
Syntax
Remove-Comments [-DestinationFilePath <String>] [-DoNotRemoveSignatureBlock] [-RemoveAllEmptyLines] [-RemoveCommentsBeforeParamBlock] [-RemoveCommentsInParamBlock] [-RemoveEmptyLines] -SourceFilePath <String> [<CommonParameters>]
#
FilePathParameters
- DestinationFilePath String
- File path to the destination file. If not provided, the content is returned.
- DoNotRemoveSignatureBlock SwitchParameter
- Do not remove a signature block, if present.
- RemoveAllEmptyLines SwitchParameter
- Remove all empty lines from the content.
- RemoveCommentsBeforeParamBlock SwitchParameter
- Remove comments before the param block. By default comments before the param block are not removed.
- RemoveCommentsInParamBlock SwitchParameter
- Remove comments in the param block. By default comments in the param block are not removed.
- RemoveEmptyLines SwitchParameter
- Remove empty lines if more than one empty line is found.
- SourceFilePath String
- File path to the source file.
Outputs
System.Object
Remove-Comments -Content <String> [-DestinationFilePath <String>] [-DoNotRemoveSignatureBlock] [-RemoveAllEmptyLines] [-RemoveCommentsBeforeParamBlock] [-RemoveCommentsInParamBlock] [-RemoveEmptyLines] [<CommonParameters>]
#
ContentParameters
- Content String
- Raw file content to process.
- DestinationFilePath String
- File path to the destination file. If not provided, the content is returned.
- DoNotRemoveSignatureBlock SwitchParameter
- Do not remove a signature block, if present.
- RemoveAllEmptyLines SwitchParameter
- Remove all empty lines from the content.
- RemoveCommentsBeforeParamBlock SwitchParameter
- Remove comments before the param block. By default comments before the param block are not removed.
- RemoveCommentsInParamBlock SwitchParameter
- Remove comments in the param block. By default comments in the param block are not removed.
- RemoveEmptyLines SwitchParameter
- Remove empty lines if more than one empty line is found.
Outputs
System.Object