---
title: "How to Use PowerShell Replace to Replace Text [Examples]"
description: "Learn, step-by-step, how to use PowerShell replace to replace strings in strings, use the replace operator and use regex and more."
canonical: "https://adamtheautomator.com/powershell-replace/"
---

# How to Use PowerShell Replace to Replace Text [Examples]

> Learn, step-by-step, how to use PowerShell replace to replace strings in strings, use the replace operator and use regex and more.

Source: https://adamtheautomator.com/powershell-replace/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Use PowerShell Replace to Replace Text \[Examples\]](https://adamtheautomator.com/wp-content/uploads/2021/03/How-to-Replace-Text-with-PowerShell-Examples.jpg)

# How to Use PowerShell Replace to Replace Text \[Examples\]

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)18 March 20216 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Using PowerShell to Replace Strings: The Basics](#h-replacing-strings-in-powershell-the-basics)
*   [Using the Replace() Method](#h-using-the-replace-method)
*   [Removing Characters](#h-removing-characters)
*   [Replacing Multiple Instances](#h-replacing-multiple-instances)
*   [Using the PowerShell Replace Operator](#h-using-the-powershell-replace-operator)
*   [Removing Characters](#h-removing-characters-1)
*   [Replacing Multiple Instances](#h-replacing-multiple-instances-1)
*   [Using PowerShell Regex Replace](#h-using-powershell-regex-replace)
*   [Escaping Regex Characters](#h-escaping-regex-characters)
*   [Using Match/Capture Groups](#h-using-match-capture-groups)
*   [Using Named Match Groups](#h-using-named-match-groups)
*   [Conclusion](#h-conclusion)

Like many other languages out there, PowerShell can work with strings and text. One of those useful features is to use PowerShell to replace characters, strings, or even text inside of files.

In this tutorial, you’re going to learn how to use the PowerShell `replace()` method and PowerShell `replace` operator. The tutorial will cover the basics and even dive into some “fun” regular expressions!

You won’t need much to follow along with all of the examples in this tutorial; you’ll just need PowerShell. This tutorial examples will be using PowerShell v7.0.2 but all examples _should_ work in Windows PowerShell.

## Using PowerShell to Replace Strings: The Basics

One of the simplest cases of using [PowerShell](https://adamtheautomator.com/powershell-strings/) replace is to replace characters in strings. Let’s start there with some examples.

Related:[Concatenate, Expand, Format and All Things PowerShell Strings](https://adamtheautomator.com/powershell-strings/)

Let’s say you have a string in PowerShell with a value of `hello, world`.

```powershell
$string = 'hello, world'
```

You’d like to replace the string `hello`, inside of that string with the string `hi` to make the `$string` variable have a value of `hi, world`. To do that, PowerShell first needs to figure out where the “find” text. Once it’s found, it then replaces that text with a user-defined value.

### Using the `Replace()` Method

One of the easiest ways to replace strings in PowerShell replace command method as shown below. The `replace()` method has two arguments; the string to find and the string to replace the found text with.

As you can see below, PowerShell is _finding_ the string `hello` and _replacing_ that string with the string `hi`. The method then returns the final result which is `hi, world`.

```powershell
PS> $string.replace('hello','hi')
hi, world
```

You can call the PowerShell replace method on any string to replace any literal string with another. If the string-to-be-replaced isn’t found, the `replace()` method returns nothing.

> _You don’t need to assign a string to a variable to replace text in a string. Instead, you can invoke the `replace()` method directly on the string like: `'hello world'.replace('hello','hi')`. The tutorial is using a variable for convenience._

#### Removing Characters

Maybe you want to remove characters in a string from another string rather than replacing it with something else. You can do that too by specifying an empty string.

```powershell
PS> $string.replace('hello','')
, world
```

#### Replacing Multiple Instances

You now have the code to replace a string inside of another string. What about replacing _multiple_ strings? No problem.

Since the PowerShell replace method returns a string, to replace another instance, you can append another `replace()` method call to the end. PowerShell then invokes the `replace()` method on the output of the original.

```powershell
PS> $string.replace('hello','').replace('world','earth')
, earth
```

> _You can chain together as many `replace()` method calls as necessary but you should look into using the `replace` operator if you have many strings to replace._

### Using the PowerShell Replace Operator

Although using the PowerShell replace string method is the simplest way to replace text, you can also use the PowerShell `replace` operator. The `replace` operator is similar to the method in that you provide a string to find and replace. But, it has one big advantage; the ability to use regular expressions (regex) to find matching strings (more later).

Related:[Getting Started with PowerShell and Regex](https://adamtheautomator.com/powershell-regex/)

Using the above example, you can use the `replace` operator to replace `hello` with `hi` in a similar fashion as shown below. PowerShell performs the same steps.

```powershell
PS> $string -replace 'hello','hi'
hi, world
```

#### Removing Characters

Like the PowerShell replace method, you can also remove characters from a string using the `replace` operator. But, unlike the `replace()` method, you can also completely exclude the string as an argument to replace with and you’ll discover the same effect.

```powershell
PS> $string -replace 'hello',''
, world
PS> $string -replace 'hello'
, world
```

#### Replacing Multiple Instances

Like the `replace()` method, you can also chain together usages of the `replace` operator. Since the `replace` operator returns a string as shown below. You’ll see in the next section, your code will be cleaner using regex.

```powershell
PS> $string -replace 'hello','hi' -replace 'world','earth'
hi, earth
```

## Using PowerShell Regex Replace

As mentioned above, replacing strings in PowerShell replace method works but it’s limited. You are constrained to only using literal strings. You cannot use wildcards or regex. If you’re performing any kind of intermediate or advanced replacing, you should use the `replace` operator.

Let’s say you have a script that contains a string that is created with a variable. That string should either be `hello, world` or `hi, world.` Maybe you have had a bad day as a sysadmin and want to change the string, regardless of the value, to `goodbye, world`.

You need `hello, world` and `hi, world` to both be transformed to `goodbye, world`. To make this happen, you need to use a regular expression. You can match just about any specific pattern in text with regex.

In this example, you can use the expression `hello|hi` to match both required strings using the regex [“or” (`|`) character](https://www.ocpsoft.org/tutorials/regular-expressions/or-in-regex/) as you can see below.

```powershell
PS> 'hello, world' -replace 'hello|hi','goodbye'
goodbye, world
PS> 'hi, world' -replace 'hello|hi','goodbye'   
goodbye, world
```

Once you learn how to wield regex to find strings, you can use PowerShell to replace wildcard strings that match _any_ pattern.

### Escaping Regex Characters

In the regex example above, the string to search in did not contain any [regex special characters](https://www.regular-expressions.info/characters.html). The regular expression language has certain characters it uses that are not interpreted literally like most letters and numbers.

For example, perhaps you need to replace text in a string. That string contains a couple of regex special characters like a bracket and a question mark. You then try to replace the string `[hello]` with `goodbye` as shown below.

```powershell
PS> '[hello], world' -replace '[hello]','goodbye'
[goodbyegoodbyegoodbyegoodbyegoodbye], wgoodbyergoodbyed
```

That’s clearly not what you intended. This scenario happens when you use regex special characters inside of the string to find (`[hello]`).

To avoid this problem, you have two options. You can either _escape_ these special characters by prepending a backslash to the front of each character or use the [`Escape()` method](https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.escape?view=net-5.0).

Below you can see the effect of escaping each special character with a backslash.

```powershell
PS> '[hello], world' -replace '\[hello\]','goodbye'
goodbye, world
```

Alternatively, and recommended, you can use the regex type’s `Escape()` method to automatically remove all special characters.

```powershell
PS> '[hello], world' -replace ([regex]::Escape('[hello]')),'goodbye'
goodbye, world
```

> _You should use the `Escape()` method where possible because it will escape all special characters so you don’t have to remember them._

### Using Match/Capture Groups

In all of the previous examples, this tutorial has been using a literal string to replace another string with. You’ve been using `hi` or `goodbye`. But what if you want to use one or more characters that PowerShell found in the string to replace with? You’ll need to match or [capture groups](https://www.regular-expressions.info/refcapture.html).

Regex has a concept called capture groups and backreferences. Capture groups allow you to _capture_ strings to then reference elsewhere. PowerShell leverages this features by using match groups with the `replace` operator.

For example, perhaps you have a string that could contain a few different values.

```powershell
'hello world, you sexy beast'
'hi world, now go away'
'hello earth, you are lovely today'
```

You’d like to swap the first part of the string with the second part making them look like this:

```powershell
'you sexy beast,hello world'
'now go away,hi world'
'you are lovely today,hello earth'
```

To perform this action, PowerShell must find all of the text to the right and left of the comma. Once it knows what that text is, it must then replace one with the other. To do that, you need backreferences.

A backreference is a regex variable (not a PowerShell variable) that represents the text that regex matched. Backreferences in PowerShell are represented with a dollar sign followed by a number indicating the order in which they were matched.

You can see an example below.

```powershell
## This string could also be:
## 'hi, world, now go away'
## 'hello, earth, you are lovely today'
PS> $string = 'hello, world, you sexy beast'
PS> $string -replace '(.*), (.*)','$2,$1'
you sexy beast,hello world
```

In the above example, you can see regex capture groups enclosing each match (`hello world`) and (`you sexy beast`) with parentheses. Then, for the replacement, `hello word` was matched first from left to right so it gets a `$1` backreference label and `you sexy beast` gets a `$2` backreference label.

Once PowerShell knows the value for each match, you can use these references in the replaced text in any way you’d like. In this example `$2,$1` swaps their positions.

### Using Named Match Groups

If you’d rather not use number placeholders like `$1`, `$2` to reference match values, you can use labels or names too. Instead of having to count from left to right which references mean what, you can simply use names.

To use names as references, you need to first define labels for each match in the match string. To do that, you must define the capture group like `(?<label><regex>)` where `label` is the name and `<regex>` is the regular expression you are using.

Once you’ve defined the names, you can then reference them in the replace string using a dollar sign and enclosing the name in curly braces e.g. `${label}`.

You can see a demonstration of this technique below.

```powershell
PS> $string = 'hello, world, you sexy beast'
PS> $string -replace '(?<First_Part>.*), (?<Second_Part>.*)','${Second_Part},${First_Part}'
you sexy beast,hello, world
```

## Conclusion

As you’ve learned, PowerShell replace operator allows you to replace characters, text and strings many different ways. To perform simple replacements, you can use the `replace()` method but if you need to match and replace anything more advanced, always use the `replace` operator.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-replace%2F&text=How%20to%20Use%20PowerShell%20Replace%20to%20Replace%20Text%20%5BExamples%5D)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-replace%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-replace%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
