user829174
user829174

Reputation: 6362

Powershell replace a substring according to regex

In Powershell, how can I replace the following string

"\\10.10.10.5\Program Files (x86)\Foo"

into this:

"C:\Program Files (x86)\Foo"

I need to replace only if \\IP was detected in the beginning of the string

EDIT

The IP can be any IP so it should be detected by some RegEx in the format of \\X.X.X.X

Upvotes: 0

Views: 69

Answers (1)

Loïc MICHEL
Loïc MICHEL

Reputation: 26120

"\\10.10.10.5\Program Files (x86)\Foo" -replace "\\\\10.10.10.5", "c:"  

after your comment, please try this one :

"\\10.10.10.5\Program Files (x86)\Foo" -replace "\\\\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", "c:" 

Upvotes: 1

Related Questions