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!

Community
  • 1
  • 1
kerosene
  • 930
  • 14
  • 31

1 Answers1

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563