Author Posts

February 4, 2014 at 11:03 pm

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.

February 4, 2014 at 11:43 pm

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

February 5, 2014 at 6:30 am

$a = "\\filedep\iservershare\DCSVWA\somefilename.txt"
$a = $a -replace "\\\\filedep\\iservershare\\DCSVWA","\\EGSISFS01\VolksWagon\DCSVWA"
$a

The backslash is an escape character in Regex.

February 5, 2014 at 8:01 am

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')