Solved

PowerShell Special Characters In Text String

Posted on 2016-08-08
3
440 Views
Last Modified: 2016-08-10
I am trying to run the PowerShell script below to replace a couple of characters in a file but this script will not work with special characters,  It works fine if I try to replace characters like 'abc' instead of replacing characters like '['.  How can I make PowerShell treat the bracket characters the same way it treats all other characters?

#  Replace brackets and double pipe with a single pipe character.
$original_file = 'C:\TestIn.txt'
$destination_file =  'C:\TestOut.txt'
(Get-Content $original_file) | Foreach-Object {
    $_ -replace '[', '|' `
       -replace ']', '|' `
       -replace '||', '|'
    } | Set-Content $destination_file
0
Comment
Question by:LesterJebson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 41747985
escape the special characters
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 41748036
The -replace operator searches for match using regular expressions, so as aikimark mentioned you need to escape any characters which have special meaning in regex syntax.

The escape character is a backslash "\".  So for your specific example you would use '\['.  The closing bracket ']' can be matched either escaped or not.

One method you can use the [regex]::Escape static method, which will generate a regex string while escaping any possible special characters.
[regex]::Escape('3ksli[[kek]$-*')

Final thing to mention is that you could construct your own regex pattern to match a number of things.  The below would match any of '[', ']', '||' and replace it with "|".
$_ -replace "\[|]|\|\|", "|"

Open in new window

0
 

Author Closing Comment

by:LesterJebson
ID: 41750663
Thank you Footech!  I added the backslash as you suggested.  Then the script ran perfectly.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring Remote Assistance for use with SCCM
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question