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?
 
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
 
QlemoBatchelor, 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
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.

All Courses

From novice to tech pro — start learning today.