Link to home
Start Free TrialLog in
Avatar of HComms
HComms

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HComms
HComms

ASKER

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

Thanks again both of you
Does PowerShell can parse and save any Textfile not just .TXT file extension ?
A text file is a text file is a text file.
The file extension is just a gimmick.
Ah i see,

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

Thanks for the clarification.