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

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

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

双引号字符串

用双引号括起来的字符串是 可展开 的字符串。 在将字符串传递到命令进行处理之前,) (前面带有美元符号 $ 的变量名称将替换为变量的值。

$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.2.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.

Here-strings

此处字符串的引号规则略有不同。

here-string 是一个单引号或双引号字符串,由 () @ 的符号包围。 此处字符串中的引号按字面解释。

here-string:

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

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

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

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

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

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

    For help, type "get-help"

    此命令的输出为:

    For help, type "get-help"
    

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

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

    此命令的输出为:

    Use a quotation mark (') 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-strings 也是一种方便的输入到 ConvertFrom-StringData cmdlet 的格式,用于将 here 字符串转换为哈希表。 有关详细信息,请参阅 ConvertFrom-StringData

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

    可展开字符串的解释

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

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

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

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

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

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

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

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

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

  • about_Parsing
  • about_Special_Characters
  • ConvertFrom-StringData
  •