Challenge - 5 Problems
PowerShell Replace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
What is the output of this PowerShell command using -replace?
Consider the following command:
What will be the output?
$text = 'Hello World!'; $text -replace 'World', 'PowerShell'
What will be the output?
PowerShell
$text = 'Hello World!'; $text -replace 'World', 'PowerShell'
Attempts:
2 left
💡 Hint
The -replace operator replaces the first string with the second in the original text.
✗ Incorrect
The -replace operator searches for the pattern 'World' and replaces it with 'PowerShell', so the output is 'Hello PowerShell!'.
💻 Command Output
intermediate1:30remaining
What does this -replace command output when using regex?
Given this command:
What is the output?
'abc123def' -replace '\d+', 'XYZ'
What is the output?
PowerShell
'abc123def' -replace '\d+', 'XYZ'
Attempts:
2 left
💡 Hint
The pattern '\d+' matches one or more digits.
✗ Incorrect
The regex '\d+' matches '123' and replaces it with 'XYZ', so the output is 'abcXYZdef'.
📝 Syntax
advanced2:00remaining
Which option correctly uses -replace to remove all vowels from a string?
You want to remove all vowels (a, e, i, o, u) from the string 'PowerShell'. Which command does this correctly?
Attempts:
2 left
💡 Hint
The -replace operator needs both a pattern and a replacement string.
✗ Incorrect
Option A correctly uses a regex pattern to match vowels and replaces them with an empty string, removing them.
🔧 Debug
advanced2:00remaining
Why does this -replace command cause an error?
Look at this command:
Why does it cause an error?
'Hello123' -replace 123, 'ABC'
Why does it cause an error?
PowerShell
'Hello123' -replace 123, 'ABC'
Attempts:
2 left
💡 Hint
Check the type of the pattern argument.
✗ Incorrect
The pattern argument must be a string or regex pattern. Using a number causes a type error.
🚀 Application
expert2:00remaining
How many replacements occur with this command?
Given this command:
How many characters are replaced?
'aaaabbbb' -replace 'a', 'x'
How many characters are replaced?
PowerShell
'aaaabbbb' -replace 'a', 'x'
Attempts:
2 left
💡 Hint
The -replace operator replaces all matches by default.
✗ Incorrect
The pattern 'a' matches all four 'a' characters and replaces each with 'x', so 4 replacements occur.