主题 about_Quoting_Rules 简短说明 介绍单引号和双引号在 Windows PowerShell 中的使用规则。 详细说明 引号用于指定文本字符串。可以将字符串括在单引号 (') 或双引号 (") 中。 引号还用于创建 here-string。here-string 是一个由单引号或双引号括起的字符串,在其中按照字面解释引 号。here-string 可包括多行。here-string 中的所有行均被解释为字符串,未括在引号中的行也如此。 在对远程计算机发出的命令中,引号定义在远程计算机上运行的命令部分。在远程会话中, 引号还用于确定命令中的变量是先在本地计算机上进行解释,还是在远程计算机上进行解释。 单引号和双引号字符串 将字符串括在双引号中时(这种字符串称为"双引号字符串"),则在将字符串传递给命令进行处理之前, 以美元符号 ($) 开头的变量名称将被替换为变量的值。 例如: $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. 将字符串括在单引号中时(这种字符串称为"单引号字符串"),该字符串将按键入时的原样传递给命令。 不执行任何替换。例如: $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. 要防止在双引号字符串中换入变量值,可使用倒引号字符 (`)(ASCII 96),该字符为 Windows PowerShell 转义字符。 在以下示例中,第一个 $i 变量前面的倒引号字符将阻止 Windows PowerShell 用该变量的值替换变量名 称。例如: $i = 5 "The value of `$i is $i." 此命令的输出是: The value $i 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 要强制 Windows PowerShell 按字面解释双引号,请使用倒引号字符。这将防止 Windows PowerShell 将引号解释为字符串分隔符。例如: "Use a quotation mark (`") to begin a string." 因为单引号字符串的内容将按照字面解释,所以不能在单引号字符串中使用倒引号字符强制按字面解释。 例如,以下命令将生成一个错误,因为 Windows PowerShell 无法识别该转义字符。Windows PowerShell 会将 第二个引号解释为字符串的结束。 PS C:\> 'Use a quotation mark (`') to begin a string.' Unexpected token ')' in expression or statement. At line:1 char:27 + 'Use a quotation mark (`') <<<< to begin a string.' 单引号和双引号 Here-String here-string 的引号使用规则略有不同。 here-string 是一个由单引号或双引号括起的字符串,在其中按照字面解释引号。here-string 可包括多 行。here-string 中的所有行均被解释为字符串,未括在引号中的行也如此。 类似于常规字符串,双引号 here-string 中的变量将使用变量值替换。在单引号 here-string 中, 将不使用变量值替换变量。 here-string 可用于任意文本,但对下列几种文本尤其有用: - 包含字面文本引号的文本 - 诸如 HTML 或 XML 文档文本等多行文本以及 - 脚本或函数的帮助文本 here-string 可以采用以下任何一种格式,其中 <Enter> 表示在按 Enter 键时添加的换行隐藏字符。 格式 1: @"<Enter> <string> [string] ...<Enter> "@ 格式 2: @'<Enter> <string> [string] ...<Enter> '@ 在每种格式中,结束引号都必须是行中的第一个字符。 here-string 包括两个隐藏字符之间的所有文本。 在 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 Windows PowerShell profile. '@ 此命令的输出是: The $profile variable contains the path of your Windows 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\User01\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. Here-string 通常用于将多行赋值给一个变量。例如,以下 here-string 将一页 XML 内容赋值给 $page 变量。 $page = [XML] @" <command:command xmlns:maml="https://schemas.microsoft.com/maml/2004/10" xmlns:command="https://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="https://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 的一种方便的输入格式,该 cmdlet 用于 将 here-string 转换为哈希表。有关详细信息,请参阅 ConvertFrom-StringData。 另请参阅 about_Escape_Characters ConvertFrom-StringData