分析时,PowerShell 首先将输入解释为表达式。 但是,当遇到命令调用时,分析将继续处于参数模式。 不带引号的非数值参数被视为字符串。 如果有包含空格(如路径)的参数,则必须将这些参数值用引号括住。 有关参数分析的详细信息,请参阅 about_Parsing “参数模式 ”部分。

引号用于指定文本字符串。 可以将字符串括在单引号 ( ' ) 或双引号 ( " ) 中。

引号还用于创建 here-string 。 here-string 是单引号或双引号字符串,其中引号按字面解释。 here-string 可以跨越多行。 here-string 中的所有行都解释为字符串,即使它们未括在引号中。

在发送给远程计算机的命令中,引号定义在远程计算机上运行的命令的各个部分。 在远程会话中,引号还确定命令中的变量是先在本地计算机上还是远程计算机上解释。

PowerShell 将智能引号(也称为版式或大引号)视为字符串的普通引号。 不要使用智能引号将字符串括起来。 编写包含智能引号的字符串时,请按照本文档字符串部分中的“包括引号” 中的指导 进行操作。 有关智能引号的详细信息,请参阅 维基百科文章 中英文 引号中的“智能引号 ”部分。

双引号字符串

括在双引号中的字符串是 可扩展 字符串。 在字符串传递给命令进行处理之前,将用变量的值替换前置美元符号 ( $ ) 的变量名称。

$i = 5
"The value of $i is $i."

此命令的输出为:

The value of 5 is 5.

此外,在双引号字符串中,将计算表达式,并在字符串中插入结果。 例如:

"The value of $(2+3) is 5."

此命令的输出为:

The value of 5 is 5.

只有基本变量引用可以直接嵌入到可扩展字符串中。 使用数组索引或成员访问的变量引用必须包含在子表达式中。 例如:

"PS version: $($PSVersionTable.PSVersion)"
PS version: 7.5.0

若要将变量名称与字符串中的后续字符分开,请将其括在大括号 ({}) 中。 如果变量名称后跟冒号 (:),则这一点尤其重要。 PowerShell 会考虑 $: 范围说明符之间的一切内容,这通常会导致解释失败。 例如,"$HOME: where the heart is." 抛出错误,但 "${HOME}: where the heart is." 按预期工作。

为防止在双引号字符串中替换变量值,请使用反引号字符 (`),即 PowerShell 转义字符。

在以下示例中,第一个 $i 变量前面的反杆字符阻止 PowerShell 将变量名称替换为其值。

$i = 5
"The value of `$i is $i."

此命令的输出为:

The value of $i is 5.

单引号字符串

用单引号括起来的字符串是逐字字符串。 该字符串在您键时会精确传递到命令。 不执行替换。

$i = 5
'The value of $i is $i.'

此命令的输出为:

The value $i is $i.

同样,不会计算单引号字符串中的表达式。 它们被解释为字符串字面量。 例如:

'The value of $(2+3) is 5.'

此命令的输出为:

The value of $(2+3) is 5.

在字符串中包含引号字符

若要使双引号出现在字符串中,请将整个字符串括在单引号中。 例如:

'As they say, "live and learn."'

此命令的输出为:

As they say, "live and learn."

还可以将单引号字符串括在双引号字符串中。 例如:

"As they say, 'live and learn.'"

此命令的输出为:

As they say, 'live and learn.'

或者,在双引号短语周围加双引号。 例如:

"As they say, ""live and learn."""

此命令的输出为:

As they say, "live and learn."

若要在单引号字符串中包含单引号,请使用第二个连续单引号。 例如:

'don''t'

此命令的输出为:

don't

若要强制 PowerShell 按字面解释双引号,请使用反引号字符。 这可防止 PowerShell 将引号解释为字符串分隔符。 例如:

"Use a quotation mark (`") to begin a string."
'Use a quotation mark (`") to begin a string.'

由于单引号字符串的内容按字面解释,因此反引号字符被视为文本字符并在输出中显示。

Use a quotation mark (") to begin a string.
Use a quotation mark (`") to begin a string.

由于 PowerShell 将智能引号(如 )解释为普通引号,因此还需要转义智能引号。

"Double ““smart quotation marks`” must be escaped in a double-quoted string."
'Single ‘‘smart quotation marks’’ must be escaped in a single-quoted string.'
Double “smart quotation marks” must be escaped in a double-quoted string.
Single ‘smart quotation marks’ must be escaped in a single-quoted string.

Here-string

here-string 的引用规则略有不同。

here-string 是用 at 符号 (@) 括住的单引号或双引号字符串。 here-string 中的引号按字面解释。

here-string:

  • 以开始标记开头,后跟换行符
  • 以换行符结尾,后跟结束标记
  • 包括作为单个字符串一部分的开始和结束标记之间的每一行
  • 与常规字符串一样,变量用双引号 here-string 替换其值。 在单引号 here-string 中,变量不会替换为其值。

    你可以对任何文本使用 here-string,但它们对于以下类型的文本特别有用:

  • 包含文本引号的文本
  • 多行文本,如 HTML 或 XML 块中的文本
  • 脚本或函数文档的帮助文本
  • here-string 可以采用以下任一格式,其中 <Enter> 表示在按 Enter 键时添加的换行符或换行隐藏字符。

    @"<Enter>
    <string> [string] ...<Enter>
    
    @'<Enter>
    <string> [string] ...<Enter>
    

    最后的换行符是结束标记的一部分。 它不会添加到 here-string 中。

    here-string 包含开始标记与结束标记之间的所有文本。 在 here-string 中,所有引号按字面解释。 例如:

    For help, type "Get-Help"

    此命令的输出为:

    For help, type "Get-Help"
    

    使用 here-string 可以简化在命令中使用字符串。 例如:

    Use a quotation mark, like ' or ", to begin a string.

    此命令的输出为:

    Use a quotation mark, like ' or ", to begin a string.
    

    在单引号 here-string 中,变量按字面解释并准确重现。 例如:

    The $PROFILE variable contains the path of your PowerShell profile.

    此命令的输出为:

    The $PROFILE variable contains the path
    of your PowerShell profile.
    

    在双引号 here-string 中,变量替换为其值。 例如:

    Even if you have not created a profile, the path of the profile file is: $PROFILE.

    此命令的输出为:

    Even if you have not created a profile,
    the path of the profile file is:
    C:\Users\User1\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.
    

    here-string 通常用于向变量分配多行。 例如,下面的 here-string 将 XML 页分配给 $page 变量。

    $page = [xml] @"
    <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10"
    xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10"
    xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
    <command:details>
            <command:name>
                   Format-Table
            </command:name>
            <maml:description>
                <maml:para>Formats the output as a table.</maml:para>
            </maml:description>
            <command:verb>format</command:verb>
            <command:noun>table</command:noun>
            <dev:version></dev:version>
    </command:details>
    </command:command>
    

    Here-string 也是用于输入 ConvertFrom-StringData cmdlet 的便捷格式,它将 here-string 转换为哈希表。 有关详细信息,请参阅 ConvertFrom-StringData

    PowerShell 允许双引号或单引号字符串跨多行,而无需使用 here-string 的 @ 语法。 但是,完整的 here-string 语法是首选用法。

    可扩展字符串的解释

    展开的字符串不一定与在控制台中看到的默认输出相同。

    集合(包括数组)通过在元素的字符串表示形式之间放置一个空格来转换为字符串。 可以通过设置首选项变量 $OFS 来指定不同的分隔符。 有关详细信息,请参阅$OFS首选项变量

    通过调用 ToString() 方法将任何其他类型的实例转换为字符串,这可能不会提供有意义的表示形式。 例如:

    "hashtable: $(@{ key = 'value' })"
    
    hashtable: System.Collections.Hashtable
    

    若要获取与控制台中的输出相同的输出,请使用通过管道传递到 Out-String 的子表达式。 如果要删除任何前导行和尾随空行,请采用 Trim() 方法。

    "hashtable:`n$((@{ key = 'value' } | Out-String).Trim())"
    
    hashtable:
    Name                           Value
    ----                           -----
    key                            value
    

    区域性设置影响字符串解释

    这些 ToString() 方法使用当前配置的区域性设置将值转换为字符串。 例如,以下 PowerShell 会话的区域性设置为 de-DEToString()当方法将值$x转换为字符串时,它将逗号 (,) 用于小数分隔符。 此外,该方法 ToString() 使用德语区域设置的适当格式将日期转换为字符串。

    PS> Get-Culture
    LCID             Name             DisplayName
    ----             ----             -----------
    1031             de-DE            German (Germany)
    PS> $x = 1.2
    PS> $x.ToString()
    PS> (Get-Date 2024-03-19).ToString()
    19.03.2024 00:00:00
    

    但是,PowerShell 在解释可扩展字符串表达式时使用固定区域性。

    PS? "$x"
    PS> "$(Get-Date 2024-03-19)"
    03/19/2024 00:00:00
    

    将带引号的字符串传递到外部命令

    某些本机命令需要包含引号字符的参数。 PowerShell 在将其传递给外部命令之前解释带引号的字符串。 此解释将删除外引号字符。

    有关此行为的详细信息,请参阅 about_Parsing 文章。

    about_Special_Characters ConvertFrom-StringData