Share via

Replace txt in alle files

a a 81 Reputation points
Sep 27, 2023, 8:18 AM

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,747 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,501 Reputation points
    Sep 27, 2023, 3:36 PM

    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: Oldest
  1. a a 81 Reputation points
    Sep 27, 2023, 3:53 PM

    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.