# $\_ -replace and powershell 2.0

**URL:** <https://forums.powershell.org/t/replace-and-powershell-2-0/1709>\
**Category:** PowerShell Help\
**Created:** [July 6, 2013, 9:48pm UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709 "2013-07-06T21:48:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliadams1981](https://avatars.discourse-cdn.com/v4/letter/o/8baadc/32.png) [@oliadams1981](https://forums.powershell.org/u/oliadams1981)\
**Post date:** [July 6, 2013, 9:48pm UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/1 "2013-07-06T21:48:06Z")

</div>

I am having problems getting the following code to work on Powershell 2.0.

```
Get-BackupData -BACKUPID $UniqueIDS |
               Select-Object @{Name='StartTime';Expression={$_.StartTime -as [Datetime]}},FilesSkipped,JobName,BytesProcessed,DeviceName,JobStatus,FilesCorrupt |
               ConvertTo-Html -Fragment |
               ForEach{

                         $_ -replace "<td>Completed</td>","<td style=' color: green'>Completed</td>"`
                            -replace "<td>3 (Completed with exceptions)</td>","<td style=' color: green'>Completed with exceptions</td>"`
                            -replace "<td>6 Completed Failed</td>","<td style=' color: Red'> Completed Failed</td>"

                      }[{pre]
```

---

<div class="post-metadata">

**Author:** ![poshoholic](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/poshoholic/32/884_2.png) [@poshoholic](https://forums.powershell.org/u/poshoholic)\
**Post date:** [July 7, 2013, 12:37am UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/2 "2013-07-07T00:37:07Z")

</div>

It would be helpful to know what specific problems you are having with your code.

Also, if you have issues with -replace, run the first of the two strings (the search string) through [regex]::Escape to see if it contains any escape characters.

For example, when I invoke this:

```
[regex]::Escape('<foo>(Completed)</foo>')
```

I get the following back:

```
<foo>\(Completed\)</foo>
```

That shows me that the round brackets are special characters in regular expressions, and they need to be escaped when I want them treated as literal characters.

Kirk out.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [July 7, 2013, 12:53am UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/3 "2013-07-07T00:53:05Z")

</div>

You might also look into the free ebook here (on the Newsletter tab), “Creating HTML Reports in PowerShell.” There’s an EnhancedHTML module that can do conditional table cell styling as it produces the HTML, so you don’t have to jury-rig a replace.

---

<div class="post-metadata">

**Author:** ![oliadams1981](https://avatars.discourse-cdn.com/v4/letter/o/8baadc/32.png) [@oliadams1981](https://forums.powershell.org/u/oliadams1981)\
**Post date:** [July 7, 2013, 3:26am UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/4 "2013-07-07T03:26:24Z")

</div>

Thanks for the replies, I didnt realise I had actually pressed post I was waiting to add more but Andy Murray was playing at wimbledon :P. The main problem I have is when moving from powershell 3 to 2 If I want to run multiple replaces on one string in version 3.0 I do it like the way shown above but in Powershell 2.0 this doesnt work. I have seen the free ebook which has given me some excellent tips, but I understood the enhanced html module would not work on version 2.0? I still support a lot of older servers especially SBS 2008 which I have read has huge problems if powershell 3.0 is installed. Basically I am looking for the easiest way to perform multiple replaces on one string in powershell 2.0.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [July 7, 2013, 3:34am UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/5 "2013-07-07T03:34:39Z")

</div>

The module should work fine on v2, actually. I don’t think I used any v3-specific functionality.

If you’re doing serialized replaces, using a method is probably easier.

```
$string.Replace($this,$that).Replace($this,$that)
```

Something along those lines. You can use the -replace operator, but you have to group them:

```
(($string -replace $this,$that) -replace $this,$that) -replace $this,$that
```

It gets uglier faster.

---

<div class="post-metadata">

**Author:** ![poshoholic](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/poshoholic/32/884_2.png) [@poshoholic](https://forums.powershell.org/u/poshoholic)\
**Post date:** [July 7, 2013, 8:05am UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/6 "2013-07-07T08:05:41Z")

</div>

Actually that’s not quite accurate. You can use multiple -replace calls in sequence in PowerShell 2.0 and PowerShell 3.0. I use -replace (I prefer the regular expression matching capabilities that come with replace), and I routinely use multiple -replace calls in sequence, often mixing -replace, -split, and -join calls up in one line to get the output I need, no brackets required.

If you switch to the System.String Replace method, be aware that it does not perform regular expression matching.

If you are having problems switching between PowerShell 3.0 and 2.0, I don’t think your use of multiple -replace calls in one line is the culprit.

---

<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:57pm UTC](https://forums.powershell.org/t/replace-and-powershell-2-0/1709/7 "2024-05-16T20:57:45Z")

</div>


