Introduction to PowerShell MultiLine Comment
PowerShell Multiple lines comment is the way of adding the description of the script, function or writing the help to describe the functionality to get a better understanding and the explanation of the script or the part of the script without executing that part and besides, it is helpful to write the comment-based help about the function by adding the syntax, parameters, examples, etc.
Syntax:
Multiline comments are described inside the block <#…#>
Comment-based help.
<#
.<help keyword>
<help content>
#>
How does multiline comment work in PowerShell?
To add the single-line comment, we use the # in front of the line, and in PowerShell 2.0 or earlier version this was the only way to add the multiple-line comments. This means if you have the description of the 10 lines then you need to put 10 times ‘#’ in front of the line.
Single line comments ‘#’ are still there for describing the small portion of the function or the script. See how the below single-line comments look like.
# This part won't be executed
Output:
Even the expressions or any syntax won’t execute inside this block.
# Get-Date
Output:
Whenever PowerShell finds the keywords # or any content between <#…#> keywords, it skips that part for the execution.
Now let say we have a script and need to describe something about it. If we are using the single-line comments, can you assume how it seems ridiculous to write every time # infornt of the line?
This is how we need to put the # sign against each line by adding simply the multiline comments <#…#> in the script, we can avoid # sign every time.
Few things to Note:
- You can’t format the fonts the comments entered inside the block. Try making fonts Bold, change the color of the font, it won’t work because it is the unique format for each editor. The color display of the comment block depends on the editor we are using and the theme you selected.
Multiline or single-line comments might look differently in PowerShell ISE and Visual Studio code. Below are the multiline comments from the PowerShell ISE and VS code.
<# This comment is from the PowerShell ISE
Font color is different than VS code
#>
From VS code with the Atom theme.
<# This comment is from the VS code
Font color is different than PowerShell ISE
#>
- You can’t pass any variable value inside the multiline comments block. For example,
$name = "PowerShell"
;
<#
This $name variable won't expand because
This is a multiline comment
#>
Output can’t expand the name.
Examples
Let us discuss examples of PowerShell MultiLine Comment.
Example #1: Adding a multiline comment to describe the script
When you write a script, you can describe the script information like version, functionality with the multiline comments as shown below and you can use it any number of times because there is no limit to it.
Here is a combination of the single and multiline comments.
<#
Script Version: 1.0
Script Author: Chirag Nagrekar
Repo Location: Company Git Location
#>
function StopServices{
<#
Using foreach loop for the script
Getting the name of the servers to stop the Spooler service
#>
foreach($server in $servers){
Stop-Service -Name Spooler -Force -Verbose
}
}
#Calling StopServices Function
StopServices
Example #2: Shortcut to add multiline comments
In the visual studio code, there is a shortcut to add the multiline comments by selecting the lines that we need to comment and then pressing the ALT + SHIFT + A button.
For example,
This is the first line
This is the second line
This is the third line
We need to comment on the above lines. First, select those lines and press ALT + SHIFT + A.
Output:
Example #3: Adding multiline comments for the help description
Multiline comments are also helpful for writing comments-based help as shown below. They are specific and described with the.HelpName.
function CheckConnectivity{
param(
[String]$computername,
[String]$Timeout
)
<#
.SYNOPSIS
This function retries the computer connectivity for the specific time.
.PARAMETER ComputerName
Remote Computer Name
.PARAMETER Timeout
Specify the timeout in seconds
.EXAMPLE
PS> CheckConnectitivy -ComputerName TestComp -Timeout 120
#>
}
When you check the help related to this function, the above comment-based help useful for describing the function.
To know more about this section, use the below link.
Conclusion
Not only PowerShell but almost all other high-level programming languages also use single and multiline comments so that when we write a script, we don’t need to print everything on the output console but we need users or scripters to understand what the particular script is for, you can mention script version. Moreover, you can add script-related help.
Recommended Articles
This is a guide to PowerShell MultiLine Comment. Here we discuss definition, syntax, and parameters, How does multiline comment works in PowerShell? examples with code implementation. You may also have a look at the following articles to learn more –