SOLVED

String replacement issue...

%3CLINGO-SUB%20id%3D%22lingo-sub-1152291%22%20slang%3D%22en-US%22%3EString%20replacement%20issue...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1152291%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%20-%20this%20issue%20has%20me%20pulling%20my%20hair%20out!%20Im%20trying%20to%20replace%20a%20string%20in%20a%20file%2C%20the%20string%20is%20%22%26nbsp%3B%7B%24MY_SCHEMA%7D%22%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24oldString%20%3D%20'%7B%24MYSCHEMA%7D'%3CBR%20%2F%3E%24newString%20%3D%20%22BRIAN%22%3CBR%20%2F%3EWrite-Host%20%22Replacing%20%24oldString%20With%20%24newString%22%3CBR%20%2F%3EGet-ChildItem%20%24directory%20-Recurse%20-Include%20%22*.%24fileExt%22%20%7C%3CBR%20%2F%3EForEach-Object%20%7B%20(Get-Content%20%24_.FullName)%20%7C%3CBR%20%2F%3EForEach-Object%20%7B%24_%20-replace%20%24oldString%2C%20%24newString%7D%20%7C%3CBR%20%2F%3ESet-Content%20%24_.FullName%20%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20is%20that%20it%20fails%20because%20there%20is%20a%20%24%20symbol%20in%20%24oldstring...%20I%20have%20tried%20various%20ways%20of%20escaping%20the%20%24%20but%20to%20no%20avail.%20Any%20clues%20please%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3A)%3C%2Fimg%3E%20Brian%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1152291%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1152496%22%20slang%3D%22en-US%22%3ERe%3A%20String%20replacement%20issue...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1152496%22%20slang%3D%22en-US%22%3E%3CP%3EInstead%20of%20using%20the%20%22-replace%22%20operator%2C%20try%20using%20the%20replace%20method%20of%20the%20string%20object.%20This%20way%20you%20don't%20have%20to%20do%20a%20second%20foreach%20either%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%24oldString%20%3D%20'%7B%24MYSCHEMA%7D'%0A%24newString%20%3D%20'BRIAN'%0AWrite-Host%20%22Replacing%20%24oldString%20With%20%24newString%22%0AGet-ChildItem%20%24directory%20-Recurse%20-Include%20%22*.%24fileExt%22%20%7C%0AForEach-Object%20%7B%20(Get-Content%20%24_.FullName).Replace(%24oldString%2C%20%24newString)%20%7C%0ASet-Content%20%24_.FullName%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F545796%22%20target%3D%22_blank%22%3E%40BrianUK%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1152566%22%20slang%3D%22en-US%22%3ERe%3A%20String%20replacement%20issue...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1152566%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20that%20-%20and%20it%20does%20look%20a%20neater%20option%20-%20but%20its%20still%20ignoring%20my%20string.%20%3B(%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1152597%22%20slang%3D%22en-US%22%3ERe%3A%20String%20replacement%20issue...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1152597%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20for%20that%20-%20there%20is%20obviously%20some%20difference%20in%20the%20way%20that%20the%20operator%20and%20and%20method%20handle%20the%20%22%24%22%20!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F261242%22%20target%3D%22_blank%22%3E%40dretzer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1152634%22%20slang%3D%22en-US%22%3ERe%3A%20String%20replacement%20issue...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1152634%22%20slang%3D%22en-US%22%3E%3CP%3EYes.%20The%20most%20relevant%20difference%20here%20is%20that%20the%20String.Replace%20method%20does%20string%20replace%20(obviously)%20and%20the%20-replace%20operator%20actually%20uses%20regex%20replace.%3C%2FP%3E%3CP%3EIf%20you%20use%20the%20regex.replace%20method%20you%20would%20see%20the%20same%20effects%20as%20with%20the%20-replace%20operator.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EString.Replace%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.string.replace%3Fview%3Dnetframework-4.8%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.string.replace%3Fview%3Dnetframework-4.8%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegex.Replace%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.text.regularexpressions.regex.replace%3Fview%3Dnetframework-4.8%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.text.regularexpressions.regex.replace%3Fview%3Dnetframework-4.8%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20can%20be%20very%20usefull%20if%20you%20want%20to%20use%20regex%20for%20your%20replace%20operations.%20But%20for%20simple%20string%20replace%2C%20you%20should%20use%20the%20method%20of%20the%20string%20object%20instead.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F545796%22%20target%3D%22_blank%22%3E%40BrianUK%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi all - this issue has me pulling my hair out! Im trying to replace a string in a file, the string is " {$MY_SCHEMA}" 

 

$oldString = '{$MYSCHEMA}'
$newString = "BRIAN"
Write-Host "Replacing $oldString With $newString"
Get-ChildItem $directory -Recurse -Include "*.$fileExt" |
ForEach-Object { (Get-Content $_.FullName) |
ForEach-Object {$_ -replace $oldString, $newString} |
Set-Content $_.FullName }

 

The problem is that it fails because there is a $ symbol in $oldstring... I have tried various ways of escaping the $ but to no avail. Any clues please?

 

:) Brian

 

 

3 Replies
Best Response confirmed by BrianUK (New Contributor)
Solution

Instead of using the "-replace" operator, try using the replace method of the string object. This way you don't have to do a second foreach either:

 

$oldString = '{$MYSCHEMA}'
$newString = 'BRIAN'
Write-Host "Replacing $oldString With $newString"
Get-ChildItem $directory -Recurse -Include "*.$fileExt" |
ForEach-Object { (Get-Content $_.FullName).Replace($oldString, $newString) |
Set-Content $_.FullName }

@BrianUK 

Thank you for that - there is obviously some difference in the way that the operator and and method handle the "$" !

 

 

@dretzer 

Yes. The most relevant difference here is that the String.Replace method does string replace (obviously) and the -replace operator actually uses regex replace.

If you use the regex.replace method you would see the same effects as with the -replace operator.

 

String.Replace:

https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8

 

Regex.Replace:

https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.replace?view=netfra...

 

It can be very usefull if you want to use regex for your replace operations. But for simple string replace, you should use the method of the string object instead.

@BrianUK