Solved

PowerShell Special Characters In Text String

Posted on 2016-08-08
3
249 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
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

860 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