$_ -replace and powershell 2.0

This topic contains 5 replies, has 3 voices, and was last updated by  Poshoholic 5 years ago.

  • Author
  • #8114

    Oliver Adams

    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 |
                             $_ -replace "Completed","Completed"`
                                -replace "3 (Completed with exceptions)","Completed with exceptions"`
                                -replace "6 Completed Failed"," Completed Failed"
  • #8115


    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:


    I get the following back:


    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.

  • #8116

    Don Jones

    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.

  • #8117

    Oliver Adams

    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.

  • #8118

    Don Jones

    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.


    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.

  • #8119


    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.

You must be logged in to reply to this topic.