How to Comment PowerShell Code?

You can comment out code in a PowerShell script for documentation or debugging purposes, just like in any other programming language. There are line comments and block comments in PowerShell.

In PowerShell v.1, only single-line comments could be used using the # (Hash) character. Use a hashtag followed by a white-space(!) for this:

# This is a comment in PowerShell. Everything from the first # until the end of the line is treated as a comment.

Hint. This is not a comment:

#This is not a PowerShell comment as there is no space after the hash.

Anything following the hashtag (#) on the same line is ignored by the PowerShell interpreter when processing code.

In newer versions of PowerShell, you can use multi-line comments using the block <# #>.

<#

This is

a multiline

comment.

#>

In the following example, we have commented out part of the code using a comment block:

$servicename = “Spooler”

if (Get-Service $servicename -ErrorAction SilentlyContinue)

{

Write-Host "$servicename exists"

<#

Get-service $servicename |Restart-Service

Write-host “Service $servicename has been restarted”

#>

}

Multiline comments are commonly used in PowerShell to add descriptive help at the beginning of a PS script file but are also used to embed comment text in a command.

You can use a block comment to insert the comment text into a PowerShell command:

Get-Content -Path <#specify the path to your file here #> C:\ps\list.txt

powershell comment block powershell comment block of code

When editing PowerShell ISE code, the commented-out code is highlighted in green.

To add a comment block in PowerShell ISE, press CTRL+J and select “Comment block” from the drop-down menu. As a result, a PowerShell block with a comment will be added to the editor pane.

powershell comment code

Also, you can select a line your want to comment out and press the Ctrl+K keyboard shortcut.

More conveniently, you can define a comment block in PowerShell ISE using the following function:

function Toggle-Comment

{

$file = $psise.CurrentFile

$text = $file.Editor.SelectedText

if ($text.StartsWith("<#")) {

$comment = $text.Substring(3).TrimEnd("#>")

}

else

{

$comment = "<#" + $text + "#>"

}

$file.Editor.InsertText($comment)

}

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Toggle Comment', { Toggle-Comment }, 'CTRL+K')

Add this function to your PowerShell profile and it will be automatically imported into your session when PowerShell ISE starts. Now just press CTRL+K to put a block comment for the selected lines of code.

comment code in powershell

If you prefer to use Visual Studio Code to edit PowerShell scripts, you can comment out one or more code lines by selecting the lines you want and pressing the keyboard shortcut “Ctrl + /” or “Alt + Shift + A” for toggling block comments.

powershell comment code

It’s a good habit to start your PowerShell scripts with a description comment block like this:

<#

.SYNOPSIS

    Short description

.DESCRIPTION

    Long description

.EXAMPLE

    PS C:\> <example usage>

    Explanation of what the example does

.INPUTS

    Inputs (if any)

.OUTPUTS

    Output (if any)

.NOTES

    General notes

#>

In Visual Studio Code, you can automatically add a default comment-based help block to your script by typing the comment-help command:

comment powershell code

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
Cyril Kardashevsky

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.