Solved

PowerShell Special Characters In Text String

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

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

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
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…

627 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