Powershell Set-Content - modify multiple files content

Hi everyone,

I would like to modify multiple text  files in the directory based on the script below.

$GETFILE=Get-Content T:\File1.txt
$GETFILE | Foreach-Object {$_ -replace "`Message-ID: <", "Message-ID: <imp180314"} | Set-Content T:\File1.txt

I've found this on stack overflow but can't combine together with "-rename"

$header=@"
text_here
"@
Get-ChildItem .\testing\ -Recurse -Filter *.csv| Foreach-Object { $header+"`n"+ (Get-Content $_.FullName | Out-String) | Set-Content -Path $_.FullName}

Thanks.
ExcelAsked:
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.

footechCommented:
...but can't combine together with "-rename"
Do you mean "-replace"?

I believe the following will work.
Get-ChildItem .\testing\ -Filter *.txt | ForEach-Object { $file = $_.fullname; (Get-Content $file) | Foreach-Object {$_ -replace "`Message-ID: <", "Message-ID: <imp180314"} | Set-Content $file } 

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
footech's code will replace each individual file. The SO fragment however combines all files found, so please report back if you need the latter, and how it should be done.
0
ExcelAuthor Commented:
Thank you Footech. It's perfect, works like a dream!
And yes I did mean "replace" :)
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.