troubleshooting Question

Replacing bunch of string characters in a text file using Powershell

Avatar of Flex Tron
Flex TronFlag for United States of America asked on
Powershell
11 Comments2 Solutions455 ViewsLast Modified:
Working on a Powershell code which will replace a set of special characters ('¿', 'Ù', 'À','Ú','³','Ä')from a text file in a folder. Each text file is 300MB in size and the characters are repeated multiple times in it. I tried doing in manually but since there are many such files, it's almost impossible. I am using a Win 7 OS and Powershell Ver 3. Attaching the code which I have.
The issue is that it creates a new file when I run the code (New_NOV_1995.txt) but it doesn't change any character in the new file as mentioned in the code. Help very much Appreciated.

$lookupTable = @{
'¿' = '|'
'Ù' = '|'
'À' = '|'
'Ú' = '|'
'³' = '|'
'Ä' = '-'
}

$original_file = 'C:\FilePath\NOV_1995.txt'
$destination_file =  'C:\FilePath\NOV_1995_NEW.txt'

Get-Content -Path $original_file | ForEach-Object {
    $line = $_

    $lookupTable.GetEnumerator() | ForEach-Object {
        if ($line -match $_.Key)
        {
            $line = $line -replace $_.Key, $_.Value
        }
    }
   $line
} | Set-Content -Path $destination_file
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros