Replace substring in PowerShell

powershell, replace

Solution

The built-in `-replace` operator allows you to use a regex for this e.g.:

C:\PS> '-content-' -replace '-([^-]+)-', '&$1&'
&content&

Note the use of single quotes is essential on the replacement string so PowerShell doesn't interpret the $1 capture group.

Problem

I have a string in the form `-content-`, and I would like to replace it with `&content&`. How can I do this with replace in PowerShell?

Original source