3

In powershell if you have a string such as

Server\MyName

how to you replace it with

MyOtherServer\AnotherName

I have tried escaping with ' and using single quotes but that doesn't seem to work

2 Answers 2

5

There is the -replace operator, but it takes a regular expression (so you have to escape backslashes):

$s -replace 'Server\\MyName', 'MyOtherServer\AnotherName'

Of course, the necessary escaping only applies to the regex, not the replacement.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I didn't realise the first argument was a regex which was the problem
2

You can also use the Replace method, it doesn't require you to escape slashes:

$s.Replace('Server\MyName','MyOtherServer\AnotherName')

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.