Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell using variables in strings passed as parameters

I have several php files in directory, I want to replace a few words in all files with different text. It's a part of my code:

$replacements_table=
("hr_table", "tbl_table"),
('$users', "tbl_users")

foreach ($file in $phpFiles){
    foreach($replacement in $replacements_table){
        (Get-Content $file) | Foreach-Object{$_ -replace $replacement} | Set-Content $file 
    }
}

It works fine for replacing "hr_table", but doesn't work at all for '$users'. Any suggestion would be nice

like image 605
Jandrejc Avatar asked Jun 21 '26 20:06

Jandrejc


1 Answers

The string is actually a regular expression and so needs to be escaped using '\'. See this thread

$replacements_table= ("hr_table", "tbl_table"), ('\$users', "tbl_users")

will work.

like image 177
Polymorphix Avatar answered Jun 24 '26 08:06

Polymorphix