Powershell script to replace characters within multiple files

I have created a Powershell script that will replace a character string with another string, this works file along as only one file is in the directory.

This is the script I am using

#Example of PowerShell replacing text
Clear-host
$Location = "C:\Users\luketr\Desktop\test\*.txt"
$Change = Get-Content $Location
$Change | ForEach-Object {$_ -Replace "'THIRD',", ""} | Set-Content $Location
$Change | ForEach-Object {$_ -Replace "'SECOND',", ""} | Set-Content $Location
$Change | ForEach-Object {$_ -Replace "'FOURTH',", ""} | Set-Content $Location

If I have more than one file in the directory the files are merged.

I have attached the files I am wanting the place the word, SECOND, THIRD and FOURTH in the separate files.

Thanks

Luke
bacotim0.20986.txt
bacotim0.21008.txt
bacotim0.21033.txt
Luke Toulmin-RotheAsked:
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.

oBdACommented:
Get-ChildItem -Path "C:\Users\luketr\Desktop\test" -Filter "*.txt" | ForEach-Object {
	(Get-Content -Path $_ -Raw -Encoding ASCII) -replace "'(SECOND|THIRD|FOURTH)',", "" | Set-Content -Path $_
}

Open in new window

0
Luke Toulmin-RotheAuthor Commented:
Thanks when I run this I get the following error.

Get-Content : A parameter cannot be found that matches parameter name 'Raw'.
At C:\Users\luketr\Desktop\Test\find and replace.ps1:2 char:28
+     (Get-Content -Path $_ -Raw <<<<  -Encoding ASCII) -replace "'(SECOND|THIRD|FOURTH)',", "" | Set-Content -Path $_
    + CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
0
oBdACommented:
Powershell 2? From 2009? Seriously?
Get-ChildItem -Path "C:\Users\luketr\Desktop\test" -Filter "*.txt" | ForEach-Object {
	([System.IO.File]::ReadAllText($_)) -replace "'(SECOND|THIRD|FOURTH)',", "" | Set-Content -Path $_
}

Open in new window

0

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
Luke Toulmin-RotheAuthor Commented:
Excellent thank you very much.
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.