When you are working with any programming language code or scripts, you might want to document some code and for that we usually use comments to make code understandable, so in this article, I have mentioned how we can comment code in powershell scripts.

Single Line Comment

To add a single line comment in Powershell, you need to start comment with "#" sign, and rest of the line is considered as comment.

Here is an example of it:

# This is a comment in PowerShell

Multi-Line Comment or Block Comment

For adding block comments or multi-line comments, while using Powershell V2 or above you can use syntax as below

  This is Multi-line
  Comments, used for
  Long -text

Embed comment text within a command

If you want you can also embed comment within a command in powerhshell, as shown in the below example scripts

 Get-Content -Path <# configuration file #> F:\Error.txt

Here is the demo output also:


In Code Multi-line Comments

Take a look at in-code multi-line comments, can be useful to explain each step of powershell script.

The multi line comment opening/close
can be also used to comment some nested code
or as an explanation for multi chained operations..
Get-Service | <# Step explanation #>
Where-Object { $_.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped } |
<# Format-Table -Property DisplayName, Status -AutoSize |#>
Out-File -FilePath Services.txt -Encoding Unicode

Powershell Comment Shortcut Code

In Visual Studio Code

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.

In PowerShell ISE

To add a comment block in PowerShell ISE, press CTRL+J and select "Comment block" from the drop-down menu.

You may also like to read:

How to Check Installed Powershell version

Run CMD Command Using Powershell

Split String into Array in Powershell

Check Visual Studio version OR VS Code version

Compare 2 Files in VS Code

Change VS Code Terminal Font Size

How to create .pfx file from certificate and private key