How to comment PowerShell Code?

You can comment out a code in a PowerShell script for documentation or debugging purposes, just like in any other programming language.

There are two comment categories in PowerShell.

  • Line comments and Block comments.

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

For Example:

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

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.

Multi-line comment

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

<#This is

A multiline



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

$service name = “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 embed comment text and also used 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

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

To add a comment block in PowerShell ISE:

  1. Press Ctrl+J

2. Select “Comment block” from the drop-down menu.

As a result, a PowerShell block with a comment will be added to the editor pane.

You can also select a line 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(“#>”)}


{$comment = “<#” + $text + “#>”}



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

Add this function to your PowerShell profile. 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.

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.

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



    Short description


    Long description


    PS C:\> <example usage>

    Explanation of what the example does


    Inputs (if any)


    Output (if any)


    General notes


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

Leave a Reply

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

Enter Captcha Here :