在Powershell中使用换行符格式化字符串

在Powershell中使用换行符格式化字符串,powershell,powershell-2.0,Powershell,Powershell 2.0,我正在使用Powershell V2.0 我的文件中有一个字符串: -PatchInfo-=-HFInfo- 我希望将-HFInfo替换为字符串1234-PatchInfo-=-HFInfo- . 替换后的文件应如下所示: -PatchInfo-=1234 -PatchInfo-=-HFInfo- 我在使用命令 gc filename.txt-replace-HFInfo-,'1234`n \-PatchInfo-\=\-HFInfo-\' 输出文件filename.txt 但执行此命令后,我无

我正在使用Powershell V2.0

我的文件中有一个字符串: -PatchInfo-=-HFInfo-

我希望将-HFInfo替换为字符串1234-PatchInfo-=-HFInfo- . 替换后的文件应如下所示: -PatchInfo-=1234 -PatchInfo-=-HFInfo-

我在使用命令 gc filename.txt-replace-HFInfo-,'1234`n \-PatchInfo-\=\-HFInfo-\' 输出文件filename.txt

但执行此命令后,我无法获得换行符。 我也尝试过其他各种可能性,但都没能成功。 有人能告诉我这里缺少什么吗

(gc filename.txt) |% {$_.replace("-HFInfo-","`"-PatchInfo-`" = `"1234`"`r`n`"-PatchInfo-`" = `"-HFInfo-`"")} | Out-File filename.txt
将产生:

"-PatchInfo-" = "1234"
"-PatchInfo-" = "-HFInfo-"

文件中的输出为-PatchInfo-=1234rn \-PatchInfo-\=\-HFInfo-\在您的答案中遗漏的1234之前添加'。