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

lab*_*nth 913 syntax powershell commenting powershell-2.0

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

JPB*_*anc 1221

在PowerShell V1中,只有#在评论后才能生成文本.

# This is a comment in Powershell
Run Code Online (Sandbox Code Playgroud)

在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
{}
Run Code Online (Sandbox Code Playgroud)

有关详细信息.SYNOPSIS,.*请参阅about_Comment_Based_Help.

备注:这些功能的意见是由使用的Get-Helpcmdlet,并可以把关键字之前Function,或内部{}之前或代码本身之后.

  • 不知道<##>块评论.不错 (44认同)
  • 您可以在此处找到PowerShell v3的语法:http://www.microsoft.com/en-us/download/details.aspx?id = 36389.看看"B.1.2评论"部分. (3认同)

ada*_*ich 97

你使用这样的哈希标记

# This is a comment in Powershell
Run Code Online (Sandbox Code Playgroud)

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

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


Ale*_*der 33

单行注释以井号开头,#将忽略右侧的所有内容:

# Comment Here
Run Code Online (Sandbox Code Playgroud)

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

<# 
  Multi 
  Line 
#> 
Run Code Online (Sandbox Code Playgroud)

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

Get-Content -Path <# configuration file #> C:\config.ini
Run Code Online (Sandbox Code Playgroud)

注意:由于PowerShell支持Tab完成,因此Space + TAB在注释之前需要注意复制和粘贴.

  • +1用于显示块注释样式,请在一行内使用_。我来这里的目的是如何暂时注释掉在一行中声明的数组的各个元素。 (2认同)

Vic*_*Vic 15

这里

# Single line comment in Powershell

<# 
--------------------------------------
Multi-line comment in PowerShell V2+ 
-------------------------------------- 
#>
Run Code Online (Sandbox Code Playgroud)

  • 这对现有答案有何影响? (11认同)
  • 保持简单直接 (5认同)
  • `--------------------------------------` 看起来像这里的注释语法,但不是! (3认同)

Mar*_*ndl 14

在PowerShell ISE中,您可以按Ctrl+ J打开" 开始截图" 菜单并选择" 注释"块:

在此输入图像描述


Car*_*ten 8

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

 # Comment here
Run Code Online (Sandbox Code Playgroud)

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

例如,这不是评论

#requires -runasadmin
Run Code Online (Sandbox Code Playgroud)


KUT*_*ime 5

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

目前(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
Run Code Online (Sandbox Code Playgroud)

多行注释

<#
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.
#>
Run Code Online (Sandbox Code Playgroud)

嵌套多行注释

<#
Nope, these are not allowed in PowerShell.

<# This will break your first multiline comment block... #>
...and this will throw a syntax error.
#>
Run Code Online (Sandbox Code Playgroud)

代码中嵌套多行注释

<#
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
Run Code Online (Sandbox Code Playgroud)

边缘案例场景

# 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.
Run Code Online (Sandbox Code Playgroud)

  • 这不会为已有的答案添加任何新内容。 (8认同)
  • 问题询问“如何在 PowerShell 中注释掉代码”,这是演示如何注释掉代码的唯一答案。所有其他答案仅演示如何在 PowerShell 中插入注释。这不是问的问题。 (2认同)
  • @TylerH“在代码中嵌套多行注释”肯定是一个新事物,很少有人知道,其他答案也没有这么说 (2认同)