1

I have some config files structured like:

PATH_KEY=C:\\dir\\project

foo=bar

I want to write a small script that replaces a certain key with current folder. So basically I'm trying to replace "PATH_KEY=..." with "PATH_KEY=$PSScriptRoot"

My code so far:

$cfgs = Get-Childitem $PSScriptRoot -Filter *name*.cfg

foreach ($cfg in $cfgs) 
{
  (  Get-Content $cfg) -replace 'PATH_KEY=.*?\n','PATH_KEY=$PSScriptRoot' | Set-Content $cfg
}

But the regular expression to take everything till end of line is not working. Any help is appreciated!

7
  • 2
    is not working how? Why not use a mere '(?m)^PATH_KEY=.*' or even 'PATH_KEY=.*'? Commented May 9, 2016 at 9:25
  • 1
    BTW, the replacement should be PATH_KEY=$$PSScriptRoot Commented May 9, 2016 at 9:43
  • thanks it worked. only thing now is that $PSScriptRoot is not using double backslash... Commented May 9, 2016 at 10:18
  • What do you mean by a double backslash? There is no backslash in $PSScriptRoot. Commented May 9, 2016 at 10:25
  • echo $PSScriptRoot -> C:\something echo $PSScriptRoot.replace('\','\\') -> C:\\something Commented May 9, 2016 at 12:22

1 Answer 1

1

You can use

'(?m)^PATH_KEY=.*' 

or even

'PATH_KEY=.*'

Note that $ in the replacement should be doubled to denote a single $, but it is not a problem unless there is a digit after it.

See the demo:

enter image description here

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

Comments

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.