Home » PowerShell » PowerShell – Multiline Comments

PowerShell – Multiline Comments

In PowerShell, use the # symbol at the beginning of a line to comment out a single line. Multiline comments in PowerShell can be easily achieved by using <# at the beginning of the line and #> at the end of the code.

Proper use of comment-out code makes code easier for maintenance and other developers to understand the code.

The comment block of code is used to document the program to provide explanatory information about the code. PowerShell multiline comment out code summarizes the code.

In this article, I will explain how to comment out multiple lines of code in the PowerShell script and comment out a single line in PowerShell.

PowerShell – Multiline Comments

To use multiline comments in PowerShell script, use <# at beginning of the code and end the comment with #>

<# Below PowerShell script is used to 
get free disk space available on
specified ComputerName
#>

Get-CimInstance -ComputerName corp-in-18 win32_logicaldisk | `
where caption -eq "C:" | `
foreach-object {write " $($_.caption) $('{0:N2}' -f `
 ($_.Size/1gb)) GB total, $('{0:N2}' -f  `
 ($_.FreeSpace/1gb)) GB free "}

In the above PowerShell multiline comment example, a multiline comment block is added in the script using <# and ends with #> to provide explanatory information about the script to get free disk space available on the computer.

Cool Tip: How to use a multiline command in PowerShell!

PowerShell – Single Line Comment

To comment out a single line of code, use the # symbol at the beginning of the line.

Refer PowerShell single line comment example,

# Below PowerShell script is used to get free disk space available

Get-CimInstance -ComputerName corp-in-18 win32_logicaldisk | `
where caption -eq "C:" | `
foreach-object {write " $($_.caption) $('{0:N2}' -f `
 ($_.Size/1gb)) GB total, $('{0:N2}' -f  `
 ($_.FreeSpace/1gb)) GB free "}

Cool Tip: How to add a newline to string or variable PowerShell!

Conclusion

I hope you found the above article helpful on using multiline comment in PowerShell using <# and #>, comment out a single line, and comment block in PowerShell.

To comment out a single line, use the # symbol at the beginning of the code.

Use <# and #> to comment out multiple lines of code or to provide information in the comment block.

Read here about how to create a multiline string in PowerShell!

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