0

I'd like to replace a word inside a .cfg file with the name of the current user.

I know $env:username outputs the current user.

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.

1 Answer 1

2

Just replace your literal string 'current_user' with $env:username and you're good to 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:

(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"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.