在PowerShell中,单引号(')和双引号(“)之间没有这种区别。它类似于Python之类的编程语言。我们通常使用两个引号来打印语句。
PS C:\> Write-Output 'This will be printed using Single quote' This will be printed using Single quote PS C:\> Write-Output "This will be printed using double quote" This will be printed using double quote
但是,当我们评估任何表达式或打印变量时,它会产生明显的不同。
$date = Get-Date Write-Output 'Today date is : $date' Today date is : $date Write-Output "Today date is : $date" Today date is : 11/02/2020 08:13:06
您可以在上面的示例中看到,单引号不能打印变量输出,而是打印变量的名称,而双引号可以打印变量的输出。即使我们尝试评估变量名,也不能使用单引号将其完成。
PS C:\> Write-Output 'Today date is : $($date)' Today date is : $($date)
乘法运算的另一个例子
PS C:\> Write-Output "Square of 4 : $(4*4)" Square of 4 : 16 PS C:\> Write-Output 'square of 4 : $(4*4)' square of 4 : $(4*4)
以使用数组打印多个语句为例。
$name = 'Charlie' $age = 40 $str = @" New Joinee name is $name Her age is $age "@ $str
输出结果
New Joinee name is Charlie Her age is 40 He will receive 1200 dollar bonus after 2 years
上面的输出可以正确打印,但是当我们使用单引号时,它不会打印变量名。
$str = @' New Joinee name is $name Her age is $age He will receive $($age*30) dollar bonus after 2 years '@
输出结果
New Joinee name is $name Her age is $age He will receive $($age*30) dollar bonus after 2 years
结论是,仅使用单引号来打印纯文本,但要打印变量并评估字符串中的其他表达式,请在PowerShell中使用双引号。