如何在 PowerShell 中注释掉代码?

发布于 2024-12-03 11:58:57 字数 56 浏览 0 评论 0原文

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

How do you comment out code in PowerShell (1.0 or 2.0)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

如梦亦如幻 2024-12-10 11:58:58

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

# This is a comment in PowerShell

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

评论

You use the hash mark like this:

# This is a comment in PowerShell

Wikipedia has a good page for keeping track of how to do comments in several popular languages:

Comments

你与昨日 2024-12-10 11:58:58

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

# Comment Here

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

<# 
  Multi 
  Line 
#> 

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

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

注意: 因为 PowerShell 支持 制表符补全 在注释之前复制和粘贴 空格 + TAB 时需要小心。

Single line comments start with a hash symbol, everything to the right of the # will be ignored:

# Comment Here

In PowerShell 2.0 and above multi-line block comments can be used:

<# 
  Multi 
  Line 
#> 

You could use block comments to embed comment text within a command:

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

Note: Because PowerShell supports Tab Completion you need to be careful about copying and pasting Space + TAB before comments.

鲜肉鲜肉永远不皱 2024-12-10 11:58:58

这是#

有关特殊字符,请参阅PowerShell - 特殊字符和标记

It's the #.

See PowerShell - Special Characters And Tokens for special characters.

鲜肉鲜肉永远不皱 2024-12-10 11:58:58

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

输入图像描述这里

Within PowerShell ISE you can hit Ctrl+J to open the Start Snipping menu and select Comment block:

enter image description here

我一向站在原地 2024-12-10 11:58:58

这里

# Single line comment in PowerShell

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

Here

# Single line comment in PowerShell

<#
--------------------------------------
Multi-line comment in PowerShell V2+
--------------------------------------
#>
野侃 2024-12-10 11:58:58

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

 # Comment here

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

例如,这不是评论:

#requires -runasadmin

Use a hashtag followed by a white space(!) for this:

 # Comment here

Do not forget the white space here! Otherwise it can interfere with internal commands.

E.g., this is not a comment:

#requires -runasadmin
ヤ经典坏疍 2024-12-10 11:58:58

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

目前(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.

I'm a little bit late to this party but seems that nobody actually wrote all use cases. So...

Only supported version of PowerShell these days (fall of 2020 and beyond) are:

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

You don't want to or you shouldn't work with different versions of PowerShell.

Both versions (or any another version which you could come around WPS 3.0-5.0, PS Core 6.x.x on some outdated stations) share the same comment functionality.

One line comments

# 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

Multi line comments

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

Nested multi line comments

<#
Nope, these are not allowed in PowerShell.

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

In code nested multi line comments

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

Edge case scenario

# 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.
深海里的那抹蓝 2024-12-10 11:58:58

你可以做:

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

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

You can make:

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

 <#
    for more lines
    ...
    ...
    ...
    ..
    .
 #>
缱倦旧时光 2024-12-10 11:58:58

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

....
exit 

Hi
Hello
We are comments
And not executed 

exit 之后的任何内容都不会被执行,并且行为与注释非常相似。

There is a special way of inserting comments add the end of script:

....
exit 

Hi
Hello
We are comments
And not executed 

Anything after exit is not executed, and behave quite like comments.

甚是思念 2024-12-10 11:58:57

在 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 ([email protected])
    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 之前,或者放在 {} 之前或之前在代码本身之后。

In PowerShell V1 there's only # to make the text after it a comment.

# This is a comment in PowerShell

In PowerShell V2 <# #> can be used for block comments and more specifically for help comments.

#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 ([email protected])
    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
{}

For more explanation about .SYNOPSIS and .* see about_Comment_Based_Help.

Remark: These function comments are used by the Get-Help CmdLet and can be put before the keyword Function, or inside the {} before or after the code itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文