在 PowerShell 路径中转义美元符号不起作用 [英] Escaping dollar signs in PowerShell path is not working

查看:35
本文介绍了在 PowerShell 路径中转义美元符号不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不起作用?


$drvrInstFilePath = "$sharePath\$imageName\ISO`$OEM$`$1\RPKTools\RPKDriverInst.bat"
echo $drvrInstFilePath
$drvrInstContent = Get-Content -LiteralPath "$sharePath\$imageName\ISO`$OEM$`$1\RPKTools\RPKDriverInst.bat"  | Out-String

回声显示了正确的路径,但 Get-Content 命令将 $oem 和 $1 扩展为空白字符串,即使它们被转义.为什么?

The echo shows the right path, but the Get-Content command expands the $oem and $1 to blank strings, even though they are escaped. Why?

推荐答案

不要乱用转义美元符号,而是使用单引号 ' 而不是双引号 ".它可以防止 PowerShell 将 $ 扩展为变量.像这样,

Instead of messing around with escaping dollar signs, use single quotes ' instead of double quotes ". It prevents PowerShell expanding $ into a variable. Like so,

$p = "C:\temp\Share\ISO$OEM$"
# Output
C:\temp\Share\ISO$


$p = 'C:\temp\Share\ISO$OEM$'
# Output
C:\temp\Share\ISO$OEM$

如果您需要使用变量创建路径,请考虑使用Join-Path.像这样,

If you need to create a path by using variables, consider using Join-Path. Like so,

$s = "Share"
join-path "C:\temp\$s" '\ISO$OEM$' 
# Output
C:\temp\Share\ISO$OEM$

这篇关于在 PowerShell 路径中转义美元符号不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆