powershell - 如何使用 powershell 将 "\"替换为 "\\"?

标签 powershell powershell-3.0

我有一个包含路径的字符串。

$Paths = "Myfolder\Mysubfolder"

我需要将它们替换为“Myfolder\Mysubfolder”

但是 $Paths -replace "\","\\" 失败了,因为正则表达式无法找到并替换“\”。

那怎么替换呢?

最佳答案

您可以使用 .Replace(),它不使用像这样的正则表达式:

$Paths = "Myfolder\Mysubfolder"
$Paths.replace('\','\\')

要使用 -replace,您需要在匹配中转义斜杠,因为它是正则表达式,不是替换 $1$2 ...等用作替换组。

$Paths -replace '\\','\\'

两者的结果是:

Myfolder\\Mysubfolder

关于powershell - 如何使用 powershell 将 "\"替换为 "\\"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27460089/

相关文章:

powershell - 将音乐复制到iPod

powershell - 复制项目将2条语句合并为1条

Powershell 的 -split 根据方法参数的不同而表现不同

Powershell 4.0 Transcript 未捕获 Write-Host 语句的输出

Powershell - 匹配运算符仅查找第一个匹配项

powershell - 自动完成用户输入 PowerShell 2.0

powershell - 'Install-Module' : is not recognized as the name of a cmdlet, 函数、脚本文件或可运行程序运行时出错

windows - 电源外壳 3.0 : Alternative to "Get-Volume"

powershell - 如何在 powershell 中实现输出变量?

json - 在 powershell 3 中美化 json