Home » PowerShell » How to Comment Out Code in PowerShell

How to Comment Out Code in PowerShell

To comment out a single line of PowerShell script, use the # at the beginning of the line. Comment out code in PowerShell to create like other programming languages is used for documentation.

In PowerShell, you can comment out a single line or create a comment block to comment out multiple lines in the PowerShell script.

In this example, we will discuss how to comment in the PowerShell script and block comments in PowerShell.

Single Line Comments in PowerShell Script

Use the # (hash) symbol to comment out the single line in the PowerShell script. # at the beginning of the line will comment out the entire line in the script.

# Gets the members for the Get-ChildItem cmdlet in PowerShell
Get-ChildItem | Get-Member

In the above PowerShell script, comment out the first line. It uses # at the beginning.

It will execute only Get-ChildItem| Get-Member command to get all the members available for the Get-ChildItem cmdlet.

PowerShell Comment Block

Use the <# and #> symbols to create a comment block. All the lines in the comment block are interpreted as comments.

Comment block in the PowerShell can have a single line or span multiple lines.

Use the <# to start the comment and #> to end the comment.

<# Get-Content - It gets the content of the item at the location
   specifed by the path, such as text in a file or content of function.
   For files, the content is read one line at a time
 #>

Get-Content -Path D:\PS\calc.txt 

In the above PowerShell script, using the <# and #> symbols, we have commented out multiple lines.

The above script run only the Get-Content cmdlet to read the content of the file.

Comment-Based Help in PowerShell

In PowerShell, you can use <# and #> to comment out the section of comment-based Help.

In Comment-Based Help, each section is defined by keyword and dot (.) at the beginning of the keyword.

<# 
    .Synopis
        Get the square root of the number
    
    .Description
        The Get-SquareRoot cmdlet gets the number and calculate the square root

    .Parameter
        -Number <int>

    .Example
        Get-SquareRoot -Number 4

    .Syntax
        Get-SquareRoot -Number
 #>

 

In the above PowerShell script, a comment block contains help keywords like Synopsis, Description, Parameter, etc…

Conclusion

I hope the above article on comments in PowerShell using the # (hash) symbol for a single line is helpful to you.

You can comment out multiple lines in PowerShell script or Comment-Based Help in PowerShell.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page