# Replace special character in powershell

**URL:** https://forums.powershell.org/t/replace-special-character-in-powershell/2244
**Category:** PowerShell Help
**Created:** [February 4, 2014, 5:33pm UTC](https://forums.powershell.org/t/replace-special-character-in-powershell/2244 "2014-02-04T17:33:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![akash-kumar](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@akash-kumar](https://forums.powershell.org/u/akash-kumar)
#### Post date: [February 4, 2014, 5:33pm UTC](https://forums.powershell.org/t/replace-special-character-in-powershell/2244/1 "2014-02-04T17:33:24Z")

</div>

Hi All,  
I am very new on powershell forum here. For one of my daily manually find and replace task i want to create a powershell script to find and replace some network location and replace with new one.  
Like :-  
To find : \filedep\iservershare\DCSVWA  
Replace with \EGSISFS01\VolksWagon\DCSVWA

I am able to replace text without any special characters but for above I am getting regular expression error.  
And also after replacing above string i want to save result in a text file which logs records of files which have been updated.

Thanks  
Akash.

---

<div class="post-metadata">

### Author: ![akash-kumar](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@akash-kumar](https://forums.powershell.org/u/akash-kumar)
#### Post date: [February 4, 2014, 6:13pm UTC](https://forums.powershell.org/t/replace-special-character-in-powershell/2244/2 "2014-02-04T18:13:51Z")

</div>

I got the solution here:-  
$c = $c -replace [regex]::Escape(‘\filedep\iservershare\DCSVWA’),(‘\EGSISFS01\VolksWagon\DCSVWA’)

---

<div class="post-metadata">

### Author: ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)
#### Post date: [February 5, 2014, 1:00am UTC](https://forums.powershell.org/t/replace-special-character-in-powershell/2244/3 "2014-02-05T01:00:17Z")

</div>

`$a = “\filedep\iservershare\DCSVWA\somefilename.txt”
$a = $a -replace “\\filedep\iservershare\DCSVWA”,“\EGSISFS01\VolksWagon\DCSVWA”
$a
`  
The backslash is an escape character in Regex.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [February 5, 2014, 2:31am UTC](https://forums.powershell.org/t/replace-special-character-in-powershell/2244/4 "2014-02-05T02:31:16Z")

</div>

PowerShell’s -replace operator uses regular expressions, and also works well on collections. If your $c variable happens to just be a single string (not a collection of strings), though, you can also just use the Replace() method of the String class, which keeps regex out of the picture:

```
$c = $c.Replace('\\filedep\iservershare\DCSVWA', '\\EGSISFS01\VolksWagon\DCSVWA')
```

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/replace-special-character-in-powershell/2244/5 "2024-05-16T20:52:24Z")

</div>


