Powershell

How to Create Descriptive PowerShell Comments

PowerShell scripting supports comments in order to add human-readable instructions. More specifically, comments are added to the script for debugging and documentation purposes. These descriptive comments help other programmers to understand the code more clearly. Descriptive comments could be single-line or multiple-line comments.

This write-up will discuss several methods to create descriptive comments.

How to Create Descriptive PowerShell Comments?

In PowerShell, the descriptive comments can be created using the below-mentioned methods:

Method 1: Single-Line Descriptive Comments

Single-line comments can be created to add code descriptions using the “#” hashtag. The text written alongside the hashtag will be considered the comment. These comments are used to add minor code descriptions.

Example
This example will demonstrate to add a single line comment in PowerShell script:

# This is a single line comment.
Write-Host "Descriptive comment won’t display."
# Single line descriptive comment

According to the above code:

  • First, create a single-line comment using the “#” hashtag.
  • After that, specify the “Write-Host” cmdlet to add a string line.
  • Finally, add the single-line comment:

It can be observed that the single-line descriptive comment has been added successfully and it did not display in the PowerShell console.

Method 2: Multiline Descriptive Comments

Comments look good when they are short and concise, but there comes a need to write detailed descriptive comments. These comments are called the multiline or comment blocks. More specifically, Multiline comments or comment blocks start with this symbol “<#” and end with this “#>” symbol. Everything written inside these symbols will be considered as multi-line descriptive comments.

Example
This example will illustrate the creation of the multiline comments in PowerShell:

<# Description
of the code
can be added here
#>
Write-Host "This is a multi-line comment example."

According to the above code:

  • First, add the multiline comment within starting “<#” and ending “#>” symbols.
  • After that, we added the “Write-Host” cmdlet to add a string line:

The multi-line descriptive comment was added successfully, and it did not display in the PowerShell console.

Conclusion

The descriptive comment in PowerShell can be created using single-line and multiple-line comments. Single comments can be created by adding the hashtag “#” hashtag at the start of the line. While multiple-line comments are created inside the “<#” starting and the “#>” ending symbols. This blog has elaborated a detailed guide to creating descriptive PowerShell comments.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.