Skip to main content
freelanceshack.com

Back to all posts

How to Do A Conditional Replace In Powershell?

Published on
3 min read
How to Do A Conditional Replace In Powershell? image

Best PowerShell Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
4 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
5 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.51 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
6 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
7 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

BUY & SAVE
$28.99
PowerShell for Sysadmins: Workflow Automation Made Easy
8 Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis

Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis

BUY & SAVE
$22.26 $48.99
Save 55%
Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis
9 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$31.99 $44.99
Save 29%
Learn Windows PowerShell in a Month of Lunches
10 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$31.19
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
+
ONE MORE?

To do a conditional replace in PowerShell, you can use the -replace operator along with a regular expression pattern to specify the condition under which the replacement should occur.

For example, if you want to replace all occurrences of the word "old" with "new" only if it is preceded by a space, you can use the following syntax:

$text = "This is an old example." $text -replace '\s(old)', ' new'

In this example, the regular expression pattern \s(old) specifies that the word "old" should only be replaced if it is preceded by a space. The replacement text ' new' is used to replace the matched pattern with the word "new".

You can adjust the regular expression pattern and replacement text to fit the specific conditions under which you want to perform the conditional replace in PowerShell.

What is a conditional replace in PowerShell?

A conditional replace in PowerShell is a method of replacing specific values in a string based on a specified condition. This allows you to dynamically replace values in a string based on the result of evaluating a condition. This can be done using the -replace operator in PowerShell, which allows you to specify a search pattern and replacement pattern, along with a condition that determines when the replacement should occur.

How to replace a specific string in PowerShell?

You can use the -replace operator in PowerShell to replace a specific string. Here is an example of how you can use it:

# Original string $originalString = "Hello, world!"

Replace "world" with "universe"

$newString = $originalString -replace "world", "universe"

Output the new string

Write-Output $newString

In this example, the -replace operator is used to replace the word "world" with "universe" in the original string "Hello, world!". The new string "Hello, universe!" is then outputted.

How to do a conditional replace in PowerShell?

You can use the Where-Object cmdlet in PowerShell to perform a conditional replace. Here's an example of how to do a conditional replace in PowerShell:

# Define the condition for the replace operation $condition = { $_ -eq "old_value" }

Perform the conditional replace using Where-Object

Get-Content "input.txt" | ForEach-Object { if ($condition.Invoke()) { $_ -replace "old_value", "new_value" } else { $_ } } | Set-Content "output.txt"

In this example, the script reads the content of a file named "input.txt", checks if a line contains the value "old_value" and if the condition is met, replaces "old_value" with "new_value". The modified content is then written to a new file named "output.txt".