We help IT Professionals succeed at work.
Get Started

Replacing bunch of string characters in a text file using Powershell

Flex Tron
Flex Tron asked
Last Modified: 2016-02-03
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
} | Set-Content -Path $destination_file
Watch Question
Top Expert 2014
This problem has been solved!
Unlock 2 Answers and 11 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE