troubleshooting Question

Issue replace text located within file using powershell

Avatar of HComms
HComms asked on
PHPPowershell
6 Comments2 Solutions82 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros