The -replace operator helps you change parts of text easily. It finds a pattern and swaps it with new text.
You want to fix a typo in a text file.
You need to change all dates from one format to another.
You want to remove unwanted characters from a string.
You want to update words in a list of names.
You want to replace spaces with underscores in file names.
Syntax
PowerShell
'original text' -replace 'pattern', 'replacement'
The -replace operator uses regular expressions to find patterns.
It returns a new string with replacements; it does not change the original string.
Examples
This replaces the word 'world' with 'PowerShell'.
PowerShell
'hello world' -replace 'world', 'PowerShell'
This removes all non-digit characters from the phone number.
PowerShell
'123-456-7890' -replace '\D', ''
This changes commas and spaces to semicolons and spaces.
PowerShell
'apple, banana, cherry' -replace ', ', '; '
Sample Program
This script replaces 'cats' with 'birds' in the sentence.
PowerShell
$text = 'I love cats and dogs.'
$newText = $text -replace 'cats', 'birds'
Write-Output $newText
OutputSuccess
Important Notes
The -replace operator is case-insensitive by default.
To make it case-sensitive, use -creplace instead.
Remember, it works with patterns, so special characters need to be escaped.
Summary
-replace changes parts of text by matching patterns.
It uses regular expressions, so it can do simple or complex replacements.
It returns a new string and does not modify the original text.
Practice
(1/5)
1. What does the PowerShell -replace operator do?
easy
A. It creates new variables.
B. It deletes files from the system.
C. It replaces parts of a string matching a pattern with new text.
D. It runs a loop over a list.
Solution
Step 1: Understand the purpose of -replace
The -replace operator is used to find text matching a pattern and replace it with new text.
Step 2: Compare options with the operator's function
Only It replaces parts of a string matching a pattern with new text. describes replacing parts of a string, which matches the operator's purpose.
Final Answer:
It replaces parts of a string matching a pattern with new text. -> Option C
Quick Check:
-replace changes text = A [OK]
Hint: Remember: -replace changes text parts by pattern [OK]
Common Mistakes:
Confusing -replace with file or variable commands
Thinking it modifies files or variables directly
Assuming it runs loops or deletes data
2. Which of the following is the correct syntax to replace 'cat' with 'dog' in the string 'I have a cat' using -replace?
easy
A. 'I have a cat' -replace('cat', 'dog')
B. 'I have a cat' -replace 'cat', 'dog'
C. 'I have a cat' -replace 'cat' 'dog'
D. 'I have a cat' replace 'cat' with 'dog'
Solution
Step 1: Recall the syntax of -replace
The correct syntax uses -replace followed by two strings separated by a comma: string -replace 'pattern', 'replacement'.
Step 2: Check each option
'I have a cat' -replace 'cat', 'dog' matches the correct syntax. Options A, B, and D have syntax errors or incorrect usage.
Final Answer:
'I have a cat' -replace 'cat', 'dog' -> Option B
Quick Check:
Correct syntax uses comma between pattern and replacement [OK]
Hint: Use comma between pattern and replacement in -replace [OK]
Common Mistakes:
Omitting the comma between pattern and replacement
Using parentheses like a function call
Writing 'replace' without the dash
3. What is the output of this PowerShell command?
'Hello 123' -replace '\d', '#'
medium
A. Hello ###
B. Hello 123
C. Hello \d
D. Error: Invalid pattern
Solution
Step 1: Understand the pattern '\d'
The pattern '\d' matches any digit character (0-9) in the string.
Step 2: Apply replacement
Each digit in '123' is replaced by '#', so '123' becomes '###'. The rest of the string remains unchanged.
Final Answer:
Hello ### -> Option A
Quick Check:
Digits replaced by '#' = Hello ### [OK]
Hint: Remember '\d' matches digits in regex for -replace [OK]
Common Mistakes:
Thinking '\d' is literal text, not a digit pattern
Expecting original string unchanged
Confusing escape sequences in regex
4. The following command throws an error. What is the problem?
'PowerShell' -replace 's',
medium
A. Missing replacement string after the comma
B. Pattern 's' is invalid
C. The string 'PowerShell' is not quoted
D. The -replace operator cannot be used on strings
Solution
Step 1: Check the -replace syntax
The -replace operator requires two arguments: a pattern and a replacement string, separated by a comma.
Step 2: Identify the error
The command ends with a comma but no replacement string, causing a syntax error.
Final Answer:
Missing replacement string after the comma -> Option A
Quick Check:
Both pattern and replacement needed [OK]
Hint: Always provide both pattern and replacement strings [OK]
Common Mistakes:
Leaving out the replacement string
Misunderstanding the comma separator
Assuming pattern alone is enough
5. You want to replace all vowels in the string 'Automation' with '*'. Which command correctly does this using -replace?
hard
A. 'Automation' -replace '[aeiou]', '*' -all
B. 'Automation' -replace 'aeiou', '*'
C. 'Automation' -replace '[aeiou]+' '*'
D. 'Automation' -replace '[aeiou]', '*'
Solution
Step 1: Understand the pattern for vowels
The pattern '[aeiou]' matches any single vowel character in the string.
Step 2: Check the replacement command
'Automation' -replace '[aeiou]', '*' uses the correct regex pattern and syntax to replace each vowel with '*'. 'Automation' -replace 'aeiou', '*' treats vowels as a literal string, not a pattern. 'Automation' -replace '[aeiou]+' '*' lacks the comma between pattern and replacement, causing a syntax error. 'Automation' -replace '[aeiou]', '*' -all uses an invalid '-all' flag.
Final Answer:
'Automation' -replace '[aeiou]', '*' -> Option D
Quick Check:
Use character class '[aeiou]' for vowels [OK]
Hint: Use square brackets for character sets in regex patterns [OK]
Common Mistakes:
Not using brackets for character sets
Trying to pass multiple arguments without comma
Adding unsupported flags like '-all'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.