0
0
PowerShellscripting~20 mins

-replace operator in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Replace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this PowerShell command using -replace?
Consider the following command:
$text = 'Hello World!'; $text -replace 'World', 'PowerShell'

What will be the output?
PowerShell
$text = 'Hello World!'; $text -replace 'World', 'PowerShell'
AHello World!
BHello PowerShell!
CPowerShell World!
DHelloWorld!
Attempts:
2 left
💡 Hint
The -replace operator replaces the first string with the second in the original text.
💻 Command Output
intermediate
1:30remaining
What does this -replace command output when using regex?
Given this command:
'abc123def' -replace '\d+', 'XYZ'

What is the output?
PowerShell
'abc123def' -replace '\d+', 'XYZ'
Aabc\d+def
Babc123def
CXYZ
DabcXYZdef
Attempts:
2 left
💡 Hint
The pattern '\d+' matches one or more digits.
📝 Syntax
advanced
2: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?
A'PowerShell' -replace '[aeiou]', ''
B'PowerShell' -replace '[aeiou]+'
C'PowerShell' -replace '[aeiou]'
D'PowerShell' -replace '', '[aeiou]'
Attempts:
2 left
💡 Hint
The -replace operator needs both a pattern and a replacement string.
🔧 Debug
advanced
2:00remaining
Why does this -replace command cause an error?
Look at this command:
'Hello123' -replace 123, 'ABC'

Why does it cause an error?
PowerShell
'Hello123' -replace 123, 'ABC'
ABecause -replace cannot handle numbers in the pattern
BBecause the replacement string is missing
CBecause the pattern must be a string or regex, not a number
DBecause the input string is not quoted
Attempts:
2 left
💡 Hint
Check the type of the pattern argument.
🚀 Application
expert
2:00remaining
How many replacements occur with this command?
Given this command:
'aaaabbbb' -replace 'a', 'x'

How many characters are replaced?
PowerShell
'aaaabbbb' -replace 'a', 'x'
A4
B8
C1
D0
Attempts:
2 left
💡 Hint
The -replace operator replaces all matches by default.