1
$file = 'c:\temp\config.ini'
# login.ruby.authentication.key=eskimopie
$pattern = [regex] "(.*?login\.ruby\.authentication\.key)=(.*?).*"
$secret = '12345678'


$text = (Get-Content -Path c:\temp\config.ini) 
$value = $text -match "$pattern"
$text -replace "$pattern",'$1=$secret' | Set-Content config.new

The problem is it replaces the line with

login.ruby.authentication.key)=$secret (in that, it actually puts $secret instead of 12345678. I am looking for how to get this to put the value of $secret instead of the word $secret.

4
  • It says Unexpected token 'secret' in expression or statement. Commented May 3, 2017 at 19:26
  • Just replace with "`$1=$secret". When used inside single quoted literals, variables do not get expanded. Commented May 3, 2017 at 19:27
  • Hmm, that works oh great one! What is the back tick doing? Is it saying do $1 interpolation before letting double quotes do its interpolation? Commented May 3, 2017 at 19:32
  • Some questions, what sense does the line $value = ... have? It's never used. Why do you double quote $pattern several times? In $pattern (.*?).* I can't find any use, how can the lazy ? distinguish between the two .* ? Commented May 3, 2017 at 20:03

1 Answer 1

4

When used inside single quoted literals, variables do not get expanded. You need to use double quoted string literal that allows string interpolation.

However, the first $ should not be expanded. To tell PS not to interpolate it, add a backtick before it. See about_Quoting_Rules:

To prevent the substitution of a variable value in a double-quoted string, use the backtick character (`)(ASCII 96), which is the Windows PowerShell escape character.

So, replace with "`$1=$secret" where `$1 will pass a literal $1 string to the regex engine, and $secret will get interpolated to 12345678 before passing it to the regex engine.

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

2 Comments

Nice, I was just looking at $1 as a string thing that needs interpolation just because it has the $ in front of it and it was getting interpolated even in the single quotes is where I was at looking at this and trying to figure out what to do. But I was way off course there and you got me back on track. I suspect it is something like the name $1 is passed into regex module and then it does stuff with it, which is why the single ticks were used. If it interpolated prior to going into the regex module, regex would not know what the heck to do with it.
Well, yes, there are 2 "interpolation" levels: one for the PowerShell, and the other for the regex engine. If you use '$1' the $ is not interpolated by PS and is treated as a literal $, so the regex engine sees $1 and processes it as a backreference. When it got $secret, it did not treat $ as part of a backreference, and just processed it literally. When you use "`$1=$secret", the first $ goes to the regex engine as a literal $, uninterpolated, so that the regex engine could process it as a backreference, and the $secret got replaced with the var value by PS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.