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

时间:2011-09-08 02:43:10

标签: powershell syntax powershell-v2.0 commenting

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

10 个答案:

答案 0 :(得分:1173)

在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之前,也可以放在代码本身之前或之后的{}之内。

答案 1 :(得分:95)

您使用这样的哈希标记

# This is a comment in Powershell

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

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

答案 2 :(得分:38)

这是#

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

答案 3 :(得分:30)

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

# Comment Here

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

<# 
  Multi 
  Line 
#> 

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

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

注意:由于PowerShell支持Tab Completion,因此您需要注意在评论之前复制和粘贴Space + TAB

答案 4 :(得分:15)

下面

# Single line comment in Powershell

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

答案 5 :(得分:13)

在PowerShell ISE中,您可以按 Ctrl + J 打开开始剪辑菜单并选择注释块

enter image description here

答案 6 :(得分:3)

你可以:

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

 <#
    for more lines
    ...
    ...
    ...
    ..
    .
 #>

答案 7 :(得分:2)

有一种特殊的插入注释方式,可以在脚本末尾添加

....
exit 

Hi
Hello
We are comments
And not executed 

exit之后的所有内容都不会执行,其行为与注释非常相似。

答案 8 :(得分:1)

为此使用主题标签,后跟一个空格(!):

 # comment here

不要忘记这里的空白!否则会干扰内部命令。

例如这不是评论:

#requires -runasadmin

答案 9 :(得分:0)

我参加这个聚会有点晚了,但似乎没有人实际写过所有用例。所以...

这些天( 2020年秋季及以后)仅受支持的PowerShell版本是:

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

您不想或不应该使用不同版本的PowerShell。

两个版本或您可能在WPS 3.0-5.0或PS Core 6.xx上出现的任何其他版本共享相同的内容评论功能。

一行评论

# 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

多行注释

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

嵌套的多行注释

<#
Nope, these are not allowed in PowerShell.

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

在代码中嵌套多行注释

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

边缘案例场景

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