• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 40
  • Last Modified:

Issue replace text located within file using powershell


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.


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
2 Solutions
Bill PrewCommented:
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

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

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
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

Senior IT System EngineerIT ProfessionalCommented:
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.
Senior IT System EngineerIT ProfessionalCommented:
Ah i see,

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

Thanks for the clarification.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Tackle projects and never again get stuck behind a technical roadblock.
Join Now