你如何在PowerShell中注释掉代码?

问题描述 投票:876回答:7

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

powershell syntax powershell-v2.0 commenting
7个回答
1198
投票

在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 ([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
{}

有关.SYNOPSIS.*的更多解释,请参阅about_Comment_Based_Help

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


96
投票

你使用这样的哈希标记

# This is a comment in Powershell

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

http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)#Comments


38
投票

这是#

有关特殊字符,请参阅PowerShell - Special Characters And Tokens


32
投票

单行注释以hash symbol开头,#右侧的所有内容都将被忽略:

# Comment Here

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

<# 
  Multi 
  Line 
#> 

您可以使用块注释在命令中嵌入注释文本:

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

注意:因为PowerShell支持Tab Completion,所以在评论之前需要注意复制和粘贴Space + TAB


16
投票

这里

# Single line comment in Powershell

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

13
投票

在PowerShell ISE中,您可以按Ctrl + J打开“开始剪切”菜单,然后选择“注释”块:

enter image description here


3
投票

你(们)能做到:

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

 <#
    for more lines
    ...
    ...
    ...
    ..
    .
 #>
© www.soinside.com 2019 - 2024. All rights reserved.