如何注释掉PowerShell中的代码?

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

963889 次浏览

你可以像这样使用哈希标记:

# This is a comment in PowerShell

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

注释

这是#

有关特殊字符,请参阅PowerShell-特殊字符和令牌

在PowerShell V1中,只有#将其后的文本作为注释。

# This is a comment in PowerShell

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

#REQUIRES -Version 2.0
<#.SYNOPSISA brief description of the function or script. This keyword can be usedonly once in each topic..DESCRIPTIONA detailed description of the function or script. This keyword can beused only once in each topic..NOTESFile Name      : xxxx.ps1Author         : J.P. Blanc (jean-paul_blanc@silogix-fr.com)Prerequisite   : PowerShell V2 over Vista and upper.Copyright 2011 - Jean Paul Blanc/Silogix.LINKScript posted over:http://silogix.fr.EXAMPLEExample 1.EXAMPLEExample 2#>Function blabla{}

有关.SYNOPSIS.*的更多说明,请参阅about_Comment_Based_Help

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

这里

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

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

# Comment Here

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

<#MultiLine#>

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

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

备注:由于PowerShell支持选项卡完成,因此您需要注意在注释之前复制和粘贴Space + TAB

您可以制作:

 (Some basic code) # Use "#" after a line and use:
<#for more lines............#>

在PowerShell ISE中,您可以点击Ctrl+J打开开始剪切菜单并选择评论框

在此处输入图片描述

有一种特殊的插入注释的方法添加脚本末尾:

....exit
HiHelloWe are commentsAnd not executed

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

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

目前仅支持的PowerShell版本(2020年及以后)是:

  • Windows PowerShell 5.1. x
  • PowerShell 7.0. x

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

两个版本或任何其他版本,您可以在一些过时的站点上围绕WPS 3.0-5.0,PS Core 6. x. x出现共享相同的评论功能。

一行评论

# 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 '# >' istreated 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... :)
#>
<#.SYNOPSISA brief description of the function or script.This keyword can be used only once in each topic.
.DESCRIPTIONA detailed description of the function or script.This keyword can be used only once in each topic.
.NOTESSome additional notes. This keyword can be used only once in each topic.This keyword can be used only once in each topic.
.LINKA link used when Get-Help with a switch -OnLine is used.This keyword can be used only once in each topic.
.EXAMPLEExample 1You can use this keyword as many as you want.
.EXAMPLEExample 2You 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/closecan be also used to comment some nested codeor 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 scriptexitWriting 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.

使用主题标签后跟空格(!):

 # Comment here

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

例如,这是没有的评论:

#requires -runasadmin