如何在PowerShell(1.0或2.0)中注释掉代码?
当前回答
我参加这个聚会有点晚了,但似乎没有人实际上写了所有的用例。所以…
目前(2020年秋季及以后)仅支持的PowerShell版本是:
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 '# >' 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.
其他回答
在PowerShell ISE中,您可以按Ctrl+J打开开始剪辑菜单并选择注释块:
是#。
关于特殊字符,请参见PowerShell -特殊字符和令牌。
有一种特殊的方法可以在脚本末尾添加注释:
....
exit
Hi
Hello
We are comments
And not executed
退出后的任何内容都不会执行,并且表现得很像注释。
我参加这个聚会有点晚了,但似乎没有人实际上写了所有的用例。所以…
目前(2020年秋季及以后)仅支持的PowerShell版本是:
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 '# >' 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.
你可以这样使用散号:
# This is a comment in PowerShell
维基百科有一个很好的页面,用来跟踪如何用几种流行语言进行评论:
评论
推荐文章
- 问号运算符是关于什么的?
- 如何使用。net 4运行时运行PowerShell ?
- 在Ruby中->运算符叫什么?
- 在PowerShell中重新加载路径
- Jenkins:注释可以添加到Jenkins文件中吗?
- 函数在PowerShell中的返回值
- 在shell脚本中使用$()而不是反引号的好处是什么?
- ASP。NET“特殊”标签
- 如何在PowerShell中输出一些东西
- 调用webrequest, POST参数
- JavaScript错误(Uncaught SyntaxError:意外的输入结束)
- 在Bash中测试非零长度字符串:[-n "$var"]或["$var"]
- 无法加载.ps1,因为在此系统上禁止执行脚本
- Shell脚本for循环语法
- 如何获得正在执行的cmdlet的当前目录