How do you comment out code in PowerShell?

923,893

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

It's the #.

See PowerShell - Special Characters And Tokens for special characters.

Solution 5

Here

# Single line comment in PowerShell

<#
--------------------------------------
Multi-line comment in PowerShell V2+
--------------------------------------
#>
Share:
923,893
labyrinth
Author by

labyrinth

My passions are literature and programming. I am working on tools for analysis of poetry using computational linguistics libraries. I'm also working on a webapp for crowdsourced categorization of literature following foundational literary critical approaches (specifically Frye's Anatomy of Criticism). I'm also working on a d3/node.js game for teaching ipv6 (to the technical RFC level). I love my current job because it allows me to study Rails, Node, and other technologies that are useful in my goal to do more digital humanities work. I work with some great people on an innovative, lean team. I love how we have the flexibility to try out just about any new programming/devops technologies as we see fit to meet the customers' needs. I think the only way my job could be better is if it allowed me to work directly in the digital humanities/computational literature field.

Updated on July 08, 2022

Comments

  • labyrinth
    labyrinth almost 2 years

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

  • Dennis G
    Dennis G almost 12 years
    didn't know about the <# #> block comment. nice
  • james.garriss
    james.garriss about 9 years
    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.
  • TZHX
    TZHX over 8 years
    What does this add to the existing answers?
  • Vic
    Vic over 8 years
    Just keeping it simple & direct
  • Davos
    Davos about 7 years
    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
  • CitizenRon
    CitizenRon almost 7 years
    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.
  • jpmc26
    jpmc26 about 6 years
    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.
  • Chris Oldwood
    Chris Oldwood about 6 years
    +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.
  • TylerH
    TylerH over 3 years
    This doesn't add anything new to the answers that already exist.
  • TylerH
    TylerH over 3 years
    This doesn't add anything to the existing answers.
  • Peter Mortensen
    Peter Mortensen over 2 years
    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).
  • Triynko
    Triynko over 2 years
    White space is NOT required after the # for it to be a comment.
  • Peter Mortensen
    Peter Mortensen over 2 years
    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?
  • Lance U. Matthews
    Lance U. Matthews over 2 years
    @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.
  • kame
    kame over 2 years
    The -------------------------------------- looks like comment syntax here, but is not!
  • Joshua K
    Joshua K about 2 years
    Thanks for covering nested comments.