PowerShell provides two types of comments called single-line comment and multiline comment. Comments are used to explain and describe PowerShell scripts properly. Comments are not executed by the PowerShell engine and omitted. The PowerShell comments can be used to describe and explain a function, line, structure, etc. Also, the PowerShell comments can be used to add notes about PowerShell scripts.
Single Line Comment
Single line comments are used to describe simple cases and add simple comments. The hash sign
# is used to create single-line comments. The dash sign is put at the start of the line and the line is interpreted as a comment.
Get-Help
# This is a comment
Add-Computer
Inline Comment
The inline comment sign dash can be used to create inline comments. Inline comments are simply comments located inside a line and the dash sign specifies the start of the comment to the end of the line. The starting part of the line is interpreted as PowerShell commands and script. In the following example, we create an inline comment in order to explain each command executed and interpreted.
Get-ComputerInfo # Print the computer information like path, name, domain etc.
Get-LocalUser # List local users in the current system
Multiline Comment
PowerShell version 2.0 and later versions provide multiline comments. Multiline comments are very useful to explain and describe PowerShell code and script in a detailed and more structured way. <#
signs are used to set the start of the multiline comment and #>
signs are used to set the end of the multiline comment.
<#
This is a multiline/block comment
This is a multiline/block comment
This is a multiline/block comment
#>
Multiline Comment with Help Keyword and Help Content
PowerShell provides detailed features about the multiline comment. The multiline comments are also used for documentation purposes for PowerShell scripts, codes, and modules. Multiline comments may provide structured data which describes Help Keyword
and Help Content
. The syntax of the multiline comment with help keyword and help content is like below.
<#
.HELP KEYWORD
HELP CONTENT
.HELP KEYWORD
HELP CONTENT
#>
- HELP KEYWORD is the keyword used to match given HELP CONTENT.
- HELP CONTENT is related with the HELP KEYWORD.
There may be more than one help keyword and help content.
In the following example, we create multiple help keywords and help contents by using a multiline content structure.
<#
.Open
The files can be opened by this way.
.Close
The files can be closed by this way.
#>
In general, the built-in and 3rd party PowerShell modules use the following multiline comment to provide helpful information.
<#
.SYNOPSIS
A brief description of the function or script.
This keyword can be used only once in each topic.
.DESCRIPTION
A detailed description of the function or script.
This keyword can be used only once in each topic.
.NOTES
Some additional notes. This keyword can be used only once in each topic.
This keyword can be used only once in each topic.
.LINK
A link used when Get-Help with a switch -OnLine is used.
This keyword can be used only once in each topic.
.EXAMPLE
Example 1
You can use this keyword as many as you want.
.EXAMPLE
Example 2
You can use this keyword as many as you want.
.EXAMPLE
Example 3
You can use this keyword as many as you want.
#>
Multiline Comment with Hash Sign
The dash sign is create for single line comment but using in multiple lines it creates a multiline comment. But managing the multiline comment with the hash sign is a bit tedius.
# Multiline comment with dash sign
# Multiline comment with dash sign
# Multiline comment with dash sign