powershell - PowerShell 路径中的转义美元符号不起作用

标签 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

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

最佳答案

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

$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。就像这样,

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

关于powershell - PowerShell 路径中的转义美元符号不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17452401/

相关文章:

string - Powershell脚本-文件命名,日期时间和FTP

powershell - 如何从命令提示符将命名数组参数传递给其中有空格的 powershell 脚本?

Powershell 选择语句

c# - 如何使用 C# Runspace 加载 powershell 模块

PowerShell Get-ChildItem 并跳过

powershell - 如何以另一个用户身份运行程序并在 powershell 中添加参数?

powershell - 通过 Graph 更新 AzureAD/O365 UPN

powershell - 为什么 Powershell Import-Csv 无法在此 CSV 文件上正常工作?

powershell - PowerShell 函数返回值的不可预测行为

powershell - 使用 Powershell 所需配置状态的应用程序池高级设置