How to Replace Placeholders in Strings Using PowerShell?

I was working on a PowerShell script recently, where I used different methods to replace placeholders in strings. I thought I would share it with you. In this tutorial, I will show you how to replace placeholders in strings using PowerShell.

To replace placeholders in strings using PowerShell, you can use the -replace operator, which utilizes regular expressions. For example, given a template string “The capital of {State} is {Capital}.”, you can replace {State} with “California” and {Capital} with “Sacramento” using the following commands: $template = $template -replace “{State}”, “California” and $template = $template -replace “{Capital}”, “Sacramento”. This will transform the template into “The capital of California is Sacramento.”

1. Using the -replace Operator

The -replace operator in PowerShell is very useful for replacing text within strings. It uses regular expressions to identify the text to be replaced, making it very flexible.

Let me show you an example.

Suppose we have a template string for a report that includes placeholders for a state name and its capital, you can write the PowerShell script like below.

$template = "The capital of {State} is {Capital}."

We want to replace {State} with “California” and {Capital} with “Sacramento”:

$template = "The capital of {State} is {Capital}."
$template = $template -replace "\{State\}", "California"
$template = $template -replace "\{Capital\}", "Sacramento"
$template

After running the above commands, the $template string will be:

"The capital of California is Sacramento."

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

PowerShell Replace Placeholders in Strings

Read Convert Strings to Lowercase or Uppercase in PowerShell

2. Using the .Replace() Method

The .Replace() method is another way to replace text in a string in PowerShell. It’s part of the .NET String class and doesn’t use regular expressions.

Let me show you an example of how to use the .replace() method to replace placeholders in Strings in PowerShell.

Consider a greeting template for a customer service email:

$emailTemplate = "Dear {FirstName}, thank you for visiting our store in {City}."

To personalize this template for a customer named John who visited the store in New York:

$emailTemplate = "Dear {FirstName}, thank you for visiting our store in {City}."
$emailTemplate = $emailTemplate.Replace("{FirstName}", "John")
$emailTemplate = $emailTemplate.Replace("{City}", "New York")
$emailTemplate

The resulting string will be:

"Dear John, thank you for visiting our store in New York."

You can check the output in the screenshot below after I executed the above PowerShell script.

Replace Placeholders in Strings Using PowerShell

Check out Count the Number of Characters in a String in PowerShell

3. Using -f (Format Operator)

The format operator -f in PowerShell is another useful method for string substitution. It allows you to insert values into a string at specified placeholders.

Here is an example. Suppose we need to generate a sales report for a specific quarter. Then, you can write the script below.

$reportTemplate = "In Q{0}, the total sales in {1} were ${2} million."
$quarter = 2
$state = "Texas"
$sales = 5.4
$report = $reportTemplate -f $quarter, $state, $sales
$report

The resulting string will be:

"In Q2, the total sales in Texas were $5.4 million."

I executed the above PowerShell script using VS code; you can see the output in the screenshot below.

How to Replace Placeholders in Strings Using PowerShell

Read Check if a String Contains Multiple Values in PowerShell

4. Using Custom Function

You can also write a custom function in PowerShell to handle multiple replacements at once.

Let me show you how to do it.

Let’s create a function that takes a template and a hashtable of replacements:

function Replace-Placeholders {
    param (
        [string]$template,
        [hashtable]$replacements
    )
    foreach ($key in $replacements.Keys) {
        $template = $template.Replace("{$key}", $replacements[$key])
    }
    return $template
}

$template = "The governor of {State} is {Governor}."
$replacements = @{
    State = "Florida"
    Governor = "Ron DeSantis"
}
$result = Replace-Placeholders -template $template -replacements $replacements

The resulting string will be:

"The governor of Florida is Ron DeSantis."

Conclusion

If you are working as a PowerShell developer, then you will get this requirement very often. In this tutorial, I have explained how to replace placeholders in strings in PowerShell using various methods. I will always suggest using the -replace operator, the .Replace() method for this.

I hope all the above examples help you to learn this completely.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.