Replace txt in alle files

a a 81 Reputation points
2023-09-27T08:18:05.7766667+00:00

Hello,

Pls

How replace txt :

cls

# start as admin

$FileList = Get-ChildItem -path "C:\MMC\*" | select FullName
 
$FileList | ForEach-Object {
    Get-Content $_.FullName -replace ("old-txt", "new-txt") | Set-Content $_.FullName
}

Thank for your help

Arnold

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,551 Reputation points
    2023-09-27T15:36:57.2366667+00:00

    As you've discovered, "-replace" isn't a parameter accepted by the Get-Content cmdlet. A pair of parentheses will fix that. Now "-replace" will operate on the list returned by Get-Content.

    Get-ChildItem -path "C:\MMC\*" |
        ForEach-Object {
            (Get-Content $_.FullName) -replace ("old-txt", "new-txt") |
                Set-Content $_.FullName
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. a a 81 Reputation points
    2023-09-27T15:53:35.1966667+00:00

    Sir,

    Many thank!

    Arnold

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.