如何将命令输出保存到变量,以及如何将该变量用作Powershell中的命令参数?

如何将命令输出保存到变量,以及如何将该变量用作Powershell中的命令参数?,powershell,file,variables,Powershell,File,Variables,我想用当前用户的名称替换.cfg文件中的单词 我知道$env:username输出当前用户 这就是我得到的: (Get-Content -path path\example.cfg -Raw) -replace 'TEST','current_user' | Set-Content path\example.cfg 我正在尝试用当前用户替换单词“TEST”。只要用$env:username替换您的文本字符串'current\u user',就可以了:) 请注意,-replace是一个正则表达式

我想用当前用户的名称替换
.cfg
文件中的单词

我知道
$env:username
输出当前用户

这就是我得到的:

(Get-Content -path path\example.cfg -Raw) -replace 'TEST','current_user' | Set-Content path\example.cfg

我正在尝试用当前用户替换单词“TEST”。

只要用
$env:username
替换您的文本字符串
'current\u user'
,就可以了:)

请注意,
-replace
是一个正则表达式运算符,因此我们甚至可以限定要查找的单词:

(Get-Content -path path\example.cfg -Raw) -replace '\bTEST\b',$env:username | Set-Content path\example.cfg

\b
在正则表达式中是“单词边界”的意思,它可以帮助您避免将
TEST
部分替换为“最热的”或“睾丸的”

只需将您的文本字符串
'current_user'
替换为
$env:username
就可以了:)

请注意,
-replace
是一个正则表达式运算符,因此我们甚至可以限定要查找的单词:

(Get-Content -path path\example.cfg -Raw) -replace '\bTEST\b',$env:username | Set-Content path\example.cfg
\b
在正则表达式中表示“单词边界”,这将帮助您避免将
TEST
部分替换为“最热的”或“睾丸的”