You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell如何输出带双引号的变量?求可行解决方法

PowerShell打印变量时保留双引号的解决方法

以下几种方法可以实现你要的输出效果:

方法1:使用反引号转义双引号

在双引号包裹的字符串中,用**反引号(`)**转义需要保留的双引号,PowerShell就会将其视为普通字符输出:

$mobilenum = "9847636782"
Write-Host "mobile number is: `"$mobilenum`""

执行后会输出:mobile number is: "9847636782"

方法2:字符串拼接

通过单引号和变量拼接的方式,直接把双引号作为普通字符加入输出内容:

$mobilenum = "9847636782"
Write-Host ('mobile number is: "' + $mobilenum + '"')

或者用空格分隔各部分(PowerShell会自动拼接):

Write-Host 'mobile number is: "' $mobilenum '"'

方法3:使用Here-String

适合需要保留复杂格式的场景,Here-String中的双引号会被原样保留,同时变量也能正常展开:

$mobilenum = "9847636782"
$output = @"
mobile number is: "$mobilenum"
"@
Write-Host $output

内容的提问来源于stack exchange,提问作者h_akm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:57:11