如何在powershell中用美元符号替换字符串

Kev*_*ter 5 powershell

在Powershell中给出以下字符串

$string = "this is a sample of 'my' text $PSP.what do you think"
Run Code Online (Sandbox Code Playgroud)

如何使用-replace函数将字符串转换为

this is a sample of 'my' text Hello.what do you think
Run Code Online (Sandbox Code Playgroud)

我显然需要以某种方式逃避字符串,而且$ PSP在我的脚本中不是声明的变量

我需要更改$ PSP的所有提及其他字符串

Sha*_*evy 9

使用反引号字符(在tab键上方):

$string = "this is a sample of 'my' text `$PSP.what do you think"
Run Code Online (Sandbox Code Playgroud)

要使用-replace运算符替换美元符号,请使用反斜杠将其转义:

"this is a sample of 'my' text `$PSP.what do you think" -replace '\$PSP', 'hello'
Run Code Online (Sandbox Code Playgroud)

或者使用string.replace方法:

$string = "this is a sample of 'my' text `$PSP.what do you think"
$string.Replace('$PSP','Hello)'
Run Code Online (Sandbox Code Playgroud)

这是'my'文本Hello的样本.你怎么看?