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

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Rich Matheisen 47,951 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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.