Solved

PowerShell Special Characters In Text String

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now