2

I have powershell file in which i have line of variable decalration as below

[string] $global:myExePath = "\\myshare\code\scripts";

I want to replace \\myshare\code\scripts with \\mynewshare\code1\psscript at runtime by executing a powershell script.

I am using
Get-Content $originalfile | ForEach-Object { $_ -replace "\\myshare\code\scripts", $mynewcodelocation.FullName } | Set-Content ($originalfile)

If am execuing { $_ -replace "scripts", $mynewcodelocation.FullName } it is working fine, but it is not working for { $_ -replace "\\myshare\code\scripts", $mynewcodelocation.FullName }

What is wrong here ?

1 Answer 1

5

'\' is a special regex character used to escape other special character.You need to double each back slash to match one back slash.

-replace "\\\\myshare\\code\\scripts",$mynewcodelocation.FullName 

When you don't know the content of a string you can use the escape method to escape a string for you:

$unc = [regex]::escape("\\myshare\code\scripts")
$unc
\\\\myshare\\code\\scripts

-replace $unc,$mynewcodelocation.FullName 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.