1

I am trying to replace a node which has domainname\username.

As it has "\" it fails in regular expression match,

Invalid regular expression pattern: l8-dc\testuser2. At C:\Users\testuser1.l8-dc\Desktop\RunTimeAccountChange\UserAccount-Replace.ps1:164 char:68 + $Searchstr.Node.'#text'= $($Searchstr.Node.'#text') -replace <<<< $OldUserName,$NewUserN ame + CategoryInfo : InvalidOperation: (l4-dc\mesuser2:String) [], RuntimeException + FullyQualifiedErrorId : InvalidRegularExpression

The following line fails actually..

$Searchstr.Node.'#text'= $($Searchstr.Node.'#text') -replace $OldUserName,$NewUserName

How to replace with regular expression match?

1
  • If the regular expression is failing, don't you think it might be wise to add the regular expression to your question? It's hard to help you fix it when you don't let us see where it's broken. Commented Aug 2, 2012 at 14:36

2 Answers 2

1

I would have to see the actual regex pattern to be sure, but I'm guessing that you need to escape a \ somewhere in it. Do this by adding another \ to it.

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

Comments

0

Try this:

$Searchstr.Node.'#text'= $($Searchstr.Node.'#text') -replace $($OldUserName),$($NewUserName)

If this doesn't work, you can try to change the script just a little to look like this:

$Domain = "MyDomain"
$OldUserName = "OldUser"
$NewUserName = "NewUesr"
$Searchstr.Node.'#text'= $($Searchstr.Node.'#text') -replace $($Domain + "\" + $OldUserName),$($Domain + "\" + $NewUserName)

I do the second one in my environment a lot, but that's because we have many test decks that are all different domains, but with the same usernames across.

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.