Powershell comments – How to use comments in PowerShell Script

Like any other programming language, PowerShell also supports single line and multi line comments. In this article, we will look into How to use Comments in PowerShell Script and also How to add comment-based help topics for Functions and Scripts in details.

Single Line Comment in PowerShell

If you would like to use Single Line comment in Powershell script or in the code block you can use it with a special character “#“. Single line comments are supported by all the PowerShell versions. You could use Single Line comments as PowerShell inline comments.

Example of Single Line Comment in PowerShell Code

Multi Line Comments in PowerShell

Multi line comments are used to add descriptive text in the PowerShell code. Anything which is added in between <# Multi line comment goes here #> is considered as multi-line block comments. You could also use this feature as PowerShell comment block to comment multiple lines of code.

Version Support – Multi Line Comments are supported in  windows PowerShell v2.0 and above. Hence if you are using windows PowerShell V1.o then you can use only single line comments.

Example of Multi Line Comments in PowerShell

PowerShell also supports Advanced commenting also called as about_Comment_Based_Help. Using this you could write the help topics for the functions and scripts.

Syntax for  Comment Based Help in Functions

Comment based help text can appear in one of the three places in the PowerShell function.

  • Beginning of the function body
  • End of the function body
  • Before the function keyword. There should not be more than 1 line break between the help text and the function keyword.

Example of Comment Based Help in Functions

In Order to get the help text for the above function, all you need to do is run below command and you will get complete help description for the function.

Syntax for Comment-Based Help in Scripts

If you are using the PowerShell script then the Comment-based help can appear in one of the three locations.

  • At the beginning of the Script File.
  • At the end of the Script. If the script is signed then place the comment-based help text in the beginning of the script file . The end of the script file is occupied by the signature block.

Note: If the first item in the Script body is a function then leave two blank line and place the comment-based help text else the help text will be interpreted as the help text for the function, not for the Script.

As shown in the Powershell Comment-Based Help text in functions here also you can add various things like Synopsis, Description, Function Params etc.

In Order to get the help text for the PowerShell script files you need to run the command as shown below. You will get complete help description for the script file.

Replace the filename with your PowerShell Script filename and execute the command to get the complete help text.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.