如何在PowerShell(1.0或2.0)中注释掉代码?
当前回答
你可以做:
(Some basic code) # Use "#" after a line and use:
<#
for more lines
...
...
...
..
.
#>
其他回答
是#。
关于特殊字符,请参见PowerShell -特殊字符和令牌。
Here
# Single line comment in PowerShell
<#
--------------------------------------
Multi-line comment in PowerShell V2+
--------------------------------------
#>
使用一个带有空格(!)的标签:
# Comment here
别忘了这里的留白!否则它会干扰内部命令。
例如,这不是评论:
#requires -runasadmin
你可以做:
(Some basic code) # Use "#" after a line and use:
<#
for more lines
...
...
...
..
.
#>
我参加这个聚会有点晚了,但似乎没有人实际上写了所有的用例。所以…
目前(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.
推荐文章
- 增强的for循环中的Null检查
- 如何在PowerShell中获得本地主机名?
- 如何从同一个YAML文件的其他地方引用YAML“设置”?
- PowerShell:如何将数组对象转换为PowerShell中的字符串?
- 从PowerShell ISE中的另一个PS1脚本调用PowerShell脚本PS1
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
- Swift:理解// MARK
- 把if-elif-else语句放在一行中?
- 在构建中编写注释的语法是什么?gradle文件?
- PowerShell等价于grep -f
- “Write-Host”,“Write-Output”,或“[console]::WriteLine”之间的区别是什么?
- Javascript函数前导bang !语法