How to Comment Out Code in PowerShell
Introduction
Knowing how to effectively comment out code is an important skill for anyone working with scripts. Commenting out code enables you to troubleshoot the code (debugging). It also can improve readability. When comments are added, people knows that the code underneath must be valuable. In this article, we’ll walk you through how to comment out code in PowerShell.
Solution
There are two kinds of comment in PowerShell:
Single Line Comments
Single line comments start with hash (#
) symbol. Following script starts with a comment followed by codes.
# Code below will test internet connection to several websites defined in the array
$argumentArray = 'www.google.com', 'www.youtube.com'
Test-Connection $argumentArray
Multi-line Block Comments
Block comments must be started with <#
and ended with #>
. Block comments enable you to comment more than one line of code.
<#
$argumentArray = 'www.google.com', 'www.youtube.com'
Test-Connection $argumentArray
#>
You can also embed block comments within a single line of command.
$argumentArray = 'www.google.com', 'www.youtube.com'
Test-Connection <# $argumentArray #> 'www.facebook.com'
Alternatively, you can apply single line comments multiple times to comment out more than one line of commands.
# $argumentArray = 'www.google.com', 'www.youtube.com'
# Test-Connection $argumentArray
Conclusion
In PowerShell, you can comment out using single line comments or multi-line block comments. You can embed block comments within a single line of command.