Issue replace text located within file using powershell

Hi

I looking for a solution to my problem using powershell. I have around 218 php files stored within a directory on my IIS server, each file contains php version 5 functions that are now decrepit in php version 7.

I am trying to find a powershell command that will go through each php file in the directory and replace the old functions with the new.

i.e

replace mysql_query with mysqli_query

I have tried the following in powershell ISE

Select-String -Path C:\inetpub\wwwroot\*.php -pattern mysql_query

This finds all the files and displays the lines in which the commands are located, I'm am struggling to find how I can use this to replace the commands with the new function.

I have tried the following but doesn't seem to work.

$old = mysql_select
$new = mysqli_select

Select-String -Path C:\inetpub\wwwroot\regtest\*.php -pattern $old | ForEach-Object  {$_ -Replace $old, $new}

I'm not the best at powershell so thought i would ask here
LVL 1
HCommsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
How about something like this:

Get-ChildItem -Path 'C:\inetpub\wwwroot\*.php' | ForEach-Object {
    (Get-Content $_ | ForEach-Object {$_ -replace "mysql_query", "mysqli_query"}) | Set-Content $_ 
  }

Open in new window


»bp
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oBdACommented:
Try it like; after a backup, obviously, or in a test folder.
The "\b" means "word boundary", it "anchors" the expression, so that it won't find something like mysql_query2
$pattern = '\bmysql_query\b'
$replaceWith = 'mysqli_query'
Get-ChildItem -Path C:\inetpub\wwwroot\*.php | ForEach-Object {
	"Processing $($_.Name) ..." | Write-Host
	@(Get-Content -Path $_.FullName) | ForEach-Object {
		$_ -replace $pattern, $replaceWith
	} | Set-Content -Path $_.FullName
}

Open in new window

1
HCommsAuthor Commented:
Thanks for the help guys, was trying all sorts today and get it solved within 5 mins here.

Thanks again both of you
0
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

Senior IT System EngineerIT ProfessionalCommented:
Does PowerShell can parse and save any Textfile not just .TXT file extension ?
0
oBdACommented:
A text file is a text file is a text file.
The file extension is just a gimmick.
2
Senior IT System EngineerIT ProfessionalCommented:
Ah i see,

So anything that can be read by human being when using notepad :-)

Thanks for the clarification.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.