如何在PowerShell(1.0或2.0)中注释掉代码?


当前回答

有一种特殊的方法可以在脚本末尾添加注释:

....
exit 

Hi
Hello
We are comments
And not executed 

退出后的任何内容都不会执行,并且表现得很像注释。

其他回答

在PowerShell V1中,只有#使注释后面的文本成为注释。

# This is a comment in PowerShell

在PowerShell V2中<# #>可以用于块注释,更具体地说,可以用于帮助注释。

#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 (jean-paul_blanc@silogix-fr.com)
    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
{}

有关. synopsis和.*的更多解释,请参阅about_Comment_Based_Help。

注意:这些函数注释由Get-Help CmdLet使用,可以放在关键字function之前,或者放在代码本身之前或之后的{}内部。

单行注释以散列符号开始,#右边的所有内容将被忽略:

# Comment Here

在PowerShell 2.0及以上版本中,可以使用多行块注释:

<# 
  Multi 
  Line 
#> 

你可以使用块注释将注释文本嵌入到命令中:

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

注意:因为PowerShell支持制表符补全,所以在注释之前要小心复制和粘贴空格+制表符。

使用一个带有空格(!)的标签:

 # Comment here

别忘了这里的留白!否则它会干扰内部命令。

例如,这不是评论:

#requires -runasadmin

Here

# Single line comment in PowerShell

<#
--------------------------------------
Multi-line comment in PowerShell V2+
--------------------------------------
#>

你可以这样使用散号:

# This is a comment in PowerShell

维基百科有一个很好的页面,用来跟踪如何用几种流行语言进行评论:

评论