# Subtle differences replace() and -replace -eq diff output

**URL:** <https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511>\
**Category:** PowerShell Help\
**Created:** [April 14, 2018, 6:30pm UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511 "2018-04-14T18:30:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnart](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gnart](https://forums.powershell.org/u/gnart)\
**Post date:** [April 14, 2018, 6:30pm UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511/1 "2018-04-14T18:30:16Z")

</div>

Appreciate an explanation in the subtle differences between “replace()” and -replace in the following code snippets, which yielded different results.  
**replace()** in code snippet A did not work.  
**-replace()** in code snippet B worked.

```
# Snippet A
foreach ($textLine in $fileContent) {
	if ($textLine -like '*memsize*') {
	    $textLine = $textLine.Replace('(\d+)','4096')
	    $NewText += $textLine
	}
	else {
		$NewText += $textLine
	}
}

# Snippet B
foreach ($textLine in $fileContent) {
	if ($textLine -like '*memsize*') {
	    $textLine = $textLine -Replace '(\d+)','4096'
	    $NewText += $textLine
	}
	else {
		$NewText += $textLine
	}
}
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 14, 2018, 7:47pm UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511/2 "2018-04-14T19:47:40Z")

</div>

As far as I know the **replace()**-method does not work with regular expressions while the **-replace** operator does.

---

<div class="post-metadata">

**Author:** ![gnart](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gnart](https://forums.powershell.org/u/gnart)\
**Post date:** [April 14, 2018, 8:11pm UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511/3 "2018-04-14T20:11:07Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [April 15, 2018, 3:17am UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511/4 "2018-04-15T03:17:39Z")

</div>

Adding to Olaf’s response.  
See the details here:

> PSTip A difference between the –replace operator and String.Replace method 'powershellmagazine.com/2012/11/12/pstip-a-difference-between-the-replace-operator-and-string-replace-method'

> Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance? 'blogs.msdn.microsoft.com/debuggingtoolbox/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance'

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [April 16, 2018, 8:08am UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511/5 "2018-04-16T08:08:47Z")

</div>

That’s like .split() and -split. The first is a .net method of the String class, and the second is a powershell operator. The second can use regular expressions.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:35pm UTC](https://forums.powershell.org/t/subtle-differences-replace-and-replace-eq-diff-output/10511/6 "2024-05-16T20:35:57Z")

</div>


