Home DevOps How to Add a Powershell Comment to your Powershell Scripts

How to Add a Powershell Comment to your Powershell Scripts

by admin

This article will take a look at the options available to make a powershell comment and powershell comment block. I went through how to make multiline comments in Python, and wanted to cover the same topic with Powershell as these are currently the two languages I spend a lot of time using.

To start, we’ll look at powershell single line comments

Single Powershell Comment Line

You can make a single line comment in Powershell by using the hash symbol. All text to the right of the symbol will be treated as a comment and will not be processed as code when running the script. Examples of using single line comments include:

# This is a comment
get-process # This command will list all processes
powershell comment

Comment Block or Multiline Comment

A common question is how to comment out a block of powershell code. You can make a powershell multiline comment by using the following syntax:

<#
   This is a 
   Powershell multiline block comment
#>

This is a little like HTML tags, where you open and close the brackets. Everything between is treated as a comment and will not be processed during the script execution.

Powershell Script Help Comments Syntax

A common example of where a comment block is commonly used is with Powershell script help comments. You may have seen a block of text such as the following in Powershell scripts you have looked at:

<#
.SYNOPSIS
    What the script does!

.DESCRIPTION
    A more detailed description of the script

.PARAMETER Param1
    Details on parameters to be used by the script.

.EXAMPLE
    Example syntax for running the script or function
    PS C:\> Example

.NOTES
    Filename: Name of the Script
Author: Author's name here!
  Modified date: yyyy-mm-dd Version 1.0 - Release notes/details #>

This is an effective way to include documentation or help/guidance inside Powershell scripts, and is good practice to include! You can read more about comment based help here. Note that Powershell help comments should be included at the beginning of your scripts.

Using Help Comments for a Powershell Function

You can also use a ‘help’ comment block for a powershell function. For example:

function get-myservices {
<#
.SYNOPSIS
    What the script does!

.DESCRIPTION
    A more detailed description of the script

.PARAMETER Param1
    Details on parameters to be used by the script.

.EXAMPLE
    Example syntax for running the script or function
    PS C:\> Example

.NOTES
    Filename: Name of the Script
    Author: Author's name here! 
    Modified date: yyyy-mm-dd
    Version 1.0 - Release notes/details
#>
get-service 
}

The above script creates a very simple function called get-myservices that just lists services on a Windows server. Because we have defined powershell help comments we can run get-help against the new get-myservices cmdlet:

powershell-multiline-comments

Using Block Comments on a Single Line of Powershell Code

Another example of where you can use block comments is in the middle of a powershell line of code. For example:

Get-Content -Path <# filename #> C:\list.txt

The <# filename #> part is a comment – note that this is in the middle of a command – but because its enclosed as a block comment, it will be ignored whilst the code to the left and right of it is processed as normal. This can be useful to give hints on what a line of code is doing, though typically I would do this by putting a single line comment above the code.

Final Thoughts…

You should now have a good understanding of how to make a single line powershell comment and how to make powershell multiline comments.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More