如何用特殊符号换行替换特殊符号行

How can I replace special symbol line by special symbol new line

我将内容存储在文本文件中。其中,第 25 行的内容如下

$userid = $null

现在我想用

替换这一行
$userid = "Chandru"

我尝试使用以下代码,但没有帮助。

$content = Get-content c:\content.text
$oldline = "`$userid = `$null"
$newline = "`$userid = `"chandru`""
$newcontent = $content -replace ("$oldline","$newline")

这对我不起作用。

"`$userid" 转义 PowerShell 的 $。您需要为正则表达式转义 $

$oldline = '$userid = $null'
$newline = '$userid = "chandru"'
(Get-Content 'C:\content.text') -replace $oldline, $newline

如果要转义字符串中的所有特殊字符,可以使用 [regex]::Escape()