Sleep or Pause to PowerShell Script

Last Updated on August 7, 2025 by Arnav Sharma

Here are some PowerShell examples with code and explanation to replace strings:

1. Basic Text Replacement

$text = "I love apple."
$newText = $text.Replace("apple", "orange")
$newText

Explanation
Here, we use the `.Replace()` method, which is an intrinsic string method in .NET (the framework upon which PowerShell is built). The method takes two arguments: the substring to find and the substring to replace it with. In this case, it finds “apple” and replaces it with “orange”, producing the string “I love orange.”

2. Case-Insensitive Replacement

$text = "I love Apple."
$newText = $text -replace "(?i)apple", "orange"
$newText

Explanation: 
PowerShell’s `-replace` operator can be combined with regular expressions for more advanced string manipulation. The `(?i)` is a regex inline modifier that makes the subsequent pattern case-insensitive. Hence, “Apple” (starting with a capital ‘A’) is matched and replaced with “orange”.

3. Replace Multiple Occurrences

$text = "apple is red. apple is tasty."
$newText = $text.Replace("apple", "orange")
$newText

Explanation: 
Again, the `.Replace()` method replaces all instances of the specified substring, not just the first one. This results in both occurrences of “apple” being replaced, producing “orange is red. orange is tasty.”

4. Replacing Text in a File

(Get-Content C:pathtofile.txt) | Foreach-Object {
    $_ -replace "apple", "orange"
} | Set-Content C:pathtofile.txt

Explanation: 
`Get-Content` reads the content of a file line by line. Each line is then piped (`|`) to `Foreach-Object`, a loop that processes each line (`$_` represents the current line). Inside this loop, the `-replace` operator replaces “apple” with “orange”. The transformed content is then piped to `Set-Content`, which writes it back to the original file.

5. Replacing Multiple Patterns

$text = "apple and banana are fruits."
$newText = $text -replace "apple|banana", "fruit"
$newText

Explanation: 
In regular expressions, the `|` symbol acts as an OR operator. The pattern “apple|banana” matches either “apple” or “banana”. Both matches are then replaced with “fruit”, resulting in “fruit and fruit are fruits.”

6. Using Capturing Groups

$text = "I love apple pie."
$newText = $text -replace "(apple) (pie)", "$2 of $1"
$newText

Explanation: 
Parentheses in regex are used to define capturing groups. The pattern `(apple) (pie)` captures “apple” as the first group and “pie” as the second. In the replacement string, `$1` refers to the first captured group and `$2` to the second. Thus, “apple pie” is rearranged to “pie of apple”.

7. Replacing Based on Conditions

$text = "Numbers 3, 7, and 4."
$newText = $text -replace "b([6-9]|d{2,})b", "big"
$newText

Explanation: 
The regex pattern is designed to match numbers greater than 5. The `b` denotes a word boundary, ensuring we match whole numbers. `[6-9]` matches numbers from 6 to 9, while `d{2,}` matches numbers with two or more digits (like 10, 11, …). Numbers matching either of these conditions are replaced with “big”.

8. Removing Text

$text = "I have 3 apples and 7 oranges."
$newText = $text -replace "d+", ""
$newText

Explanation: 
`d` is a regex metacharacter that matches any digit (0-9). The `+` quantifier means “one or more” of the preceding element. So, `d+` matches one or more digits in sequence. By replacing these matches with an empty string, we effectively remove all numbers from the original string.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.