How to Find & Replace Multiple Lines in PowerShell

You may have to convert your strings to a single line to replace them

Reading time icon 4 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more

Key notes

  • Writing PowerShell scripts can be extremely helpful in automating tasks, but having to search all over the place to replace multiple lines of text can be difficult.
  • With a few commands, we show you how to achieve this without the need to use additional tools.

Windows PowerShell is one of the most powerful command-line utilities in the OS. The problem, however, is that users have little to no information on its extensive abilities. For instance, PowerShell can find and replace multiple lines of text in a file. 

This is great for many things, including changing your code base across multiple files. In this article, we will explain how to replace multiple lines of text in a file using PowerShell.

Why do I need to replace multiple lines in PowerShell?

Multiple lines in PowerShell are a great way to make your scripts more readable. They are also useful when you want to use one statement on multiple lines but don’t want to break it up into multiple statements.

In programming, multiple lines mean writing a single command or instruction in two or more lines. 

You may be wondering why we would need multiple lines. The most obvious reason is for readability purposes and to avoid mistakes while typing long commands one by one. 

We can use multiple lines when we have more than one command to execute simultaneously. It makes our code more readable and easier to understand by other people.

While all this is great, some users prefer replacing multiple lines in PowerShell. It is not difficult to replace one line at a time, but it becomes difficult and time-consuming when it comes to replacing multiple lines at once.

How do I replace a line in PowerShell?

The replace operator in PowerShell is a handy way to replace a string with another string. For example, if you want to replace all instances of the word “test” with the word “exam,” you could use the following command:

PS> "test" -Replace "test", "exam"

The Replace method has two parameters:

  • Search string that you want to find.
  • Replacement string that you want to use instead of the search string.

From the above example, you can already tell that it will be time-consuming if you were to find and replace multiple lines.

To replace multiple lines, youโ€™ll need to use the Regex command. The PowerShell regex command has two parameters:

  • Match – Allows you to specify a pattern to search for in the input string. You may also specify multiple patterns separated by commas.
  • Replace – Replaces all occurrences of the specified match with the specified replacement value.

However, it has some limitations. It can only replace the first occurrence of a match within a line. The workaround will be to convert your code to a multiline string. 

Once you convert it, PowerShell will treat the entire input as a single string making it easier to replace.

1. Convert string

  1. Hit the Windows key, type PowerShell in the search bar, and click Run as administrator.
  2. You can use the- raw switch to convert multiple lines to one string in PowerShell. When using the -raw switch, youโ€™ll need to specify the file name.
  3. For instance, to convert, you would apply the following command: Get-Content C:\user\test1.txt -Raw
  4. This includes the file location and the file name.

2. Match string

The match parameter is designed to find one or more characters that match a specified pattern.

For example, if you want to find all the words that begin with “c”, in your string, you can use the following command:

[regex]::matches("c*") | where-object{$_ -like "*c*"}

3. Replace lines

Once you’ve found the matches where you want to replace, you can execute the replace command as follows:

PS> [regex]::Replace($str, "c", "d")

PowerShell’s find and replace method can be very useful for more technical users. Hopefully, with this post, we’ve gotten a pretty solid run down on how to find and replace a single line using the PowerShell prompt.

We now know that it can be accomplished with relative ease and that the command structure isn’t all that difficult to follow. If you’re a beginner, practice makes perfect.

Other useful PowerShell parameters you may be interested in include the PowerShell ErrorAction, which we have discussed in detail.

The Try-Catch block may also be worth your time, especially if you want to catch any errors in your code.

Feel free to add your own tips on any other useful information we may have left out.

More about the topics: PowerShell