Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PowerShell Special Characters In Text String

Posted on 2016-08-08
3
Medium Priority
?
974 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 46

Expert Comment

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

Accepted Solution

by:
footech earned 2000 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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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…
Suggested Courses

971 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