powershell - 使用字符串替换在Powershell中插入字符串 '$_'吗?

标签 powershell

我在Powershell中更换字符串时遇到麻烦。请考虑以下表达式:

"replaceTarget: %rep" -replace '%rep','$_'

我希望这个表达式的结果是:
replaceTarget: $_

而是
replaceTarget: replaceTarget: $rep

我认为这是因为替换字符串'$_'replace函数中还有其他含义。

我如何转义输入字符串,以便$_可以传递而不进行评估?

最佳答案

您可以使用.net替换功能,如下所示:'replaceTarget: $rep'.replace("`$rep",'$_')

关于powershell - 使用字符串替换在Powershell中插入字符串 '$_'吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32583315/

相关文章:

windows - 在 Windows 命令行上运行 raco

powershell - 将 [System.Drawing.Bitmap] 转换为 [File.IFileAbstraction]

powershell - 从脚本中获取函数列表

arrays - Powershell-调用每个对象的函数会更改所有对象中的数组时的奇怪行为

powershell - 您将如何生成从 000 到 9ZZ 的唯一序列

powershell - 从Invoke-Command执行的脚本中访问凭据

powershell - 了解Powershell : example - Convert JSON to CSV

powershell - 使用单个 DSC 配置初始化可变数量的磁盘

powershell - PowerShell 中的 bool 文字

powershell - 如何根据数字占位符将值转换为 KB、MB 或 GB?