1092

How do you comment out code in PowerShell (1.0 or 2.0)?

5
  • 34
    Note '#' is a comment in many shell and script languages: bash, python, php, ruby, and now powershell.
    – yzorg
    Oct 16, 2013 at 3:13
  • 79
    This is exactly why I assumed that # is not a comment in a Windows or Microsoft based scripting language. Dec 8, 2014 at 10:22
  • 4
    That's because, apparently, unlike all their other technologies, MS didn't give powershell a decent reference. I couldn't find it anywhere. Oct 3, 2016 at 12:11
  • 11
    @dudeNumber4 PowerShell has one of the most comprehensive references of any language. In order for a feature to be added to the language, it must include comprehensive help documentation with examples, method and member lists, etc.
    – TylerH
    Feb 27, 2019 at 2:36
  • 1
    Related post - Multiline comment in PowerShell
    – RBT
    Oct 31, 2021 at 13:22

10 Answers 10

1447

In PowerShell V1 there's only # to make the text after it a comment.

# This is a comment in PowerShell

In PowerShell V2 <# #> can be used for block comments and more specifically for help comments.

#REQUIRES -Version 2.0

<#
.SYNOPSIS
    A brief description of the function or script. This keyword can be used
    only once in each topic.
.DESCRIPTION
    A detailed description of the function or script. This keyword can be
    used only once in each topic.
.NOTES
    File Name      : xxxx.ps1
    Author         : J.P. Blanc ([email protected])
    Prerequisite   : PowerShell V2 over Vista and upper.
    Copyright 2011 - Jean Paul Blanc/Silogix
.LINK
    Script posted over:
    http://silogix.fr
.EXAMPLE
    Example 1
.EXAMPLE
    Example 2
#>
Function blabla
{}

For more explanation about .SYNOPSIS and .* see about_Comment_Based_Help.

Remark: These function comments are used by the Get-Help CmdLet and can be put before the keyword Function, or inside the {} before or after the code itself.

5
  • 61
    didn't know about the <# #> block comment. nice
    – Dennis G
    Jun 19, 2012 at 20:13
  • 4
    You can find the grammar for PowerShell v3 here: microsoft.com/en-us/download/details.aspx?id=36389. Look at section B.1.2 Comments. Mar 26, 2015 at 14:51
  • I was using the Send-MailMessage function, with backticks to put each paremeter on a new line, and commented out one of them ( -Bcc ) and it caused an error on the next line ( -Body : The term '-Body' is not recognized as the name of a cmdlet ...) So it seems commenting out a line in the middle of a call to a function is not supported. Maybe it's the line-continuation, maybe it's something else, either way that's not how I would expect it to work
    – Davos
    Mar 20, 2017 at 3:07
  • Commenting like that would be actually in-line. Your command would end up being parsed like: Send-MailMessage -To [email protected] #This is a comment -Subject "Help Me!" etc.
    – CitizenRon
    May 25, 2017 at 20:05
  • I've found it more reliable to place the function comment immediately following the opening { (inside the function). In particular, I had trouble making it work outside with script module functions.
    – jpmc26
    Apr 13, 2018 at 23:07
117

You use the hash mark like this:

# This is a comment in PowerShell

Wikipedia has a good page for keeping track of how to do comments in several popular languages:

Comments

0
61

Single line comments start with a hash symbol, everything to the right of the # will be ignored:

# Comment Here

In PowerShell 2.0 and above multi-line block comments can be used:

<# 
  Multi 
  Line 
#> 

You could use block comments to embed comment text within a command:

Get-Content -Path <# configuration file #> C:\config.ini

Note: Because PowerShell supports Tab Completion you need to be careful about copying and pasting Space + TAB before comments.

1
  • 7
    +1 for showing the block comment style use within a single line. I came here looking for how to temporarily comment out individual elements of an array all declared on one line. Apr 23, 2018 at 9:46
41

It's the #.

See PowerShell - Special Characters And Tokens for special characters.

0
24

Here

# Single line comment in PowerShell

<#
--------------------------------------
Multi-line comment in PowerShell V2+
--------------------------------------
#>
3
  • 14
    What does this add to the existing answers?
    – TZHX
    Sep 17, 2015 at 10:50
  • 8
    Just keeping it simple & direct
    – Vic
    Sep 17, 2015 at 11:26
  • 3
    The -------------------------------------- looks like comment syntax here, but is not!
    – kame
    Dec 21, 2021 at 16:48
23

Within PowerShell ISE you can hit Ctrl+J to open the Start Snipping menu and select Comment block:

enter image description here

0
8

Use a hashtag followed by a white space(!) for this:

 # Comment here

Do not forget the white space here! Otherwise it can interfere with internal commands.

E.g., this is not a comment:

#requires -runasadmin
4
  • Can you provide an explanation of what happens in the example (#requires -runasadmin)? In what way does it interfere with internal commands? Please respond by editing your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Jul 31, 2021 at 15:23
  • White space is NOT required after the # for it to be a comment.
    – Triynko
    Oct 18, 2021 at 17:20
  • Isn't this only true for CMD/batch (REM is an actual command, not syntax. E.g., "make sure your comments do not contain any % characters"), not PowerShell? Oct 20, 2021 at 12:15
  • 1
    @PeterMortensen That's a #Requires statement, which allows a script to specify some of its prerequisites. This answer seems to be suggesting to always make a space the first character of your comments so as to avoid a comment unintentionally being treated as a #Requires statement; not only does that seem incredibly unlikely, if a #Requires statement is unsatisfied or of incorrect format then an error is thrown, anyways. Dec 5, 2021 at 10:13
6

I'm a little bit late to this party but seems that nobody actually wrote all use cases. So...

Only supported version of PowerShell these days (fall of 2020 and beyond) are:

  • Windows PowerShell 5.1.x
  • PowerShell 7.0.x.

You don't want to or you shouldn't work with different versions of PowerShell.

Both versions (or any another version which you could come around WPS 3.0-5.0, PS Core 6.x.x on some outdated stations) share the same comment functionality.

One line comments

# Get all Windows Service processes <-- one line comment, it starts with '#'
Get-Process -Name *host*

Get-Process -Name *host* ## You could put as many ### as you want, it does not matter

Get-Process -Name *host* # | Stop-Service # Everything from the first # until end of the line is treated as comment

Stop-Service -DisplayName Windows*Update # -WhatIf # You can use it to comment out cmdlet switches

Multi line comments

<#
Everyting between '< #' and '# >' is
treated as a comment. A typical use case is for help, see below.

# You could also have a single line comment inside the multi line comment block.
# Or two... :)

#>

<#
.SYNOPSIS
    A brief description of the function or script.
    This keyword can be used only once in each topic.

.DESCRIPTION
    A detailed description of the function or script.
    This keyword can be used only once in each topic.

.NOTES
    Some additional notes. This keyword can be used only once in each topic.
    This keyword can be used only once in each topic.

.LINK
    A link used when Get-Help with a switch -OnLine is used.
    This keyword can be used only once in each topic.

.EXAMPLE
    Example 1
    You can use this keyword as many as you want.

.EXAMPLE
    Example 2
    You can use this keyword as many as you want.
#>

Nested multi line comments

<#
Nope, these are not allowed in PowerShell.

<# This will break your first multiline comment block... #>
...and this will throw a syntax error.
#>

In code nested multi line comments

<#
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

Edge case scenario

# Some well written script
exit
Writing something after exit is possible but not recommended.
It isn't a comment.
Especially in Visual Studio Code, these words baffle PSScriptAnalyzer.
You could actively break your session in VS Code.
5
  • 8
    This doesn't add anything new to the answers that already exist.
    – TylerH
    Oct 29, 2020 at 20:04
  • 2
    The question asked "How do you comment out code in PowerShell", and this is the only answer that demonstrates how to comment out code. All of the other answers only demonstrate how to insert comments in PowerShell. That's not the question that was asked.
    – Ray Depew
    Jul 25, 2022 at 15:31
  • 2
    @TylerH In code nested multi line comments is certainly a new thing rarely who knows and other answers don't say it Dec 3, 2022 at 16:14
  • And yet the requester did not ask "How do comments work in PowerShell."
    – Ray Depew
    Dec 5, 2022 at 1:44
  • @RayDepew Maybe yes, but Google navigates you here, so this question servers as a general knowledge about comments in PowerShell. You may not like it, you may not agree with it, but that's about it.
    – KUTlime
    Dec 5, 2022 at 7:24
3

You can make:

 (Some basic code) # Use "#" after a line and use:

 <#
    for more lines
    ...
    ...
    ...
    ..
    .
 #>
1
  • 2
    This doesn't add anything to the existing answers.
    – TylerH
    Oct 30, 2020 at 14:02
2

There is a special way of inserting comments add the end of script:

....
exit 

Hi
Hello
We are comments
And not executed 

Anything after exit is not executed, and behave quite like comments.

Not the answer you're looking for? Browse other questions tagged or ask your own question.