PowerShell Replace with Regular Expressions (regex)

In PowerShell, we can manipulate strings using regular expressions (regex). Regular expressions are patterns used to match character combinations in strings, and in PowerShell, they can be incredibly useful for text processing tasks. In this tutorial, we’ll explore how to use PowerShell’s -replace operator with regex to handle a variety of common scenarios.

PowerShell Replace Regex

The -replace operator in PowerShell uses regex to identify and replace text. The basic syntax is 'string' -replace 'pattern', 'replacement'. The pattern is the regex that you want to match, and the replacement is the text you want to insert in place of the matched text.

Example:

'Sample123Text' -replace '\d+', '456'

This will replace the sequence of digits (\d+) in ‘Sample123Text’ with ‘456’, resulting in ‘Sample456Text’.

PowerShell Replace Regex in File

To replace text in a file, you can read the file content, use the -replace operator, and then write the content back to the file.

Example:

$content = Get-Content 'example.txt'
$newContent = $content -replace 'oldText', 'newText'
$newContent | Set-Content 'example.txt'

This reads ‘example.txt’, replaces ‘oldText’ with ‘newText’, and saves the changes back to the file.

PowerShell Replace Regex in Multiple Files

To perform replacements across multiple files, you can loop through the files and apply the -replace operator to each one.

Example:

Get-ChildItem 'C:\Logs\*.log' | ForEach-Object {
    $content = Get-Content $_.FullName
    $content -replace 'error', 'warning' | Set-Content $_.FullName
}

This script changes the word ‘error’ to ‘warning’ in all ‘.log’ files in ‘C:\Logs’.

PowerShell Replace Regex String

When working with strings, you can use the -replace operator directly.

Example:

$text = 'Use PowerShell'
$text -replace 'PowerShell', 'Regex'

This will change the string to ‘Use Regex’.

PowerShell Replace Regex Multiline

By default, PowerShell’s -replace operator works with multiline strings. If you need to match the beginning (^) or end ($) of each line, use the (?m) inline option.

Example:

$multiLineText = "first line`nsecond line"
$multiLineText -replace '(?m)^', 'Line: '

This prefixes each line in the string with ‘Line: ‘.

PowerShell Replace Regex Wildcard

In regex, the . character is the wildcard that matches any single character. To match multiple characters, use .* or .+ . in PowerShell.

Example:

'abc123xyz' -replace 'b.*2', 'd456'

This will result in ‘ad4563xyz’.

PowerShell Replace Regex Variable

You can store patterns and replacements in variables and use them with the -replace operator.

Example:

$pattern = 'dog'
$replacement = 'cat'
'The quick brown dog' -replace $pattern, $replacement

This will output ‘The quick brown cat’.

PowerShell Replace Regex Newline

To replace newlines, you can use the \r (carriage return) and \n (line feed) escape sequences.

Example:

$text = "line1`r`nline2"
$text -replace "`r`n", ', '

This will replace the newline with a comma and a space, resulting in ‘line1, line2’.

PowerShell Replace Regex Escape

If you need to match a character that has a special meaning in regex, you must escape it with a backslash (\).

Example:

'Cost: $100' -replace '\$', 'USD '

This will change the string to ‘Cost: USD 100’.

Conclusion

Regular expressions in PowerShell are a potent combination for any scripting task that involves text manipulation. We’ve seen how the -replace operator can be used to perform simple and complex string replacements, whether it’s within a string, a file, or across multiple files. With the ability to handle multiline strings, incorporate wildcards, utilize variables, and even manage newlines and escape sequences.

You may also like:

>