[英]How do I save a command output to a variable, and how do I use that variable as command parameter in Powershell?
I'd like to replace a word inside a .cfg
file with the name of the current user.我想用当前用户的名称替换
.cfg
文件中的一个单词。
I know $env:username
outputs the current user.我知道
$env:username
输出当前用户。
This is what I got:这就是我得到的:
(Get-Content -path path\example.cfg -Raw) -replace 'TEST','current_user' | Set-Content path\example.cfg
I'm trying to replace the word "TEST" with the current user.我正在尝试用当前用户替换单词“TEST”。
Just replace your literal string 'current_user'
with $env:username
and you're good to go:)只需将您的文字字符串
'current_user'
替换$env:username
,您就可以使用 go :)
(Get-Content -path path\example.cfg -Raw) -replace 'TEST',$env:username | Set-Content path\example.cfg
Note that -replace
is a regex operator, so we can even qualify the word we're looking for:请注意
-replace
是一个正则表达式运算符,因此我们甚至可以限定我们正在寻找的单词:
(Get-Content -path path\example.cfg -Raw) -replace '\bTEST\b',$env:username | Set-Content path\example.cfg
\b
means "word boundary" in regex, and will help you avoid partially replacing TEST
in words like "HOTTEST" or "TESTICULAR" \b
表示正则表达式中的“单词边界”,将帮助您避免用“HOTTEST”或“TESTICULAR”等词部分替换TEST
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.