Challenge - 5 Problems
PowerShell Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell function call?
Consider this PowerShell function definition and call:
function Get-Square {
param($number)
return $number * $number
}
Get-Square 4PowerShell
function Get-Square { param($number) return $number * $number } Get-Square 4
Attempts:
2 left
💡 Hint
Think about what multiplying a number by itself means.
✗ Incorrect
The function Get-Square takes a number and returns its square. Calling Get-Square 4 returns 4 * 4 = 16.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a PowerShell function with two parameters?
Select the correct syntax for a PowerShell function named 'Add-Numbers' that takes two parameters and returns their sum.
Attempts:
2 left
💡 Hint
Remember the param block syntax inside a function.
✗ Incorrect
Option A correctly uses 'param' with parentheses and commas to define two parameters inside the function block.
🔧 Debug
advanced2:00remaining
Why does this PowerShell function fail to return the expected value?
Look at this function and its call:
function Multiply {
param($x, $y)
$result = $x * $y
}
Multiply 3 5PowerShell
function Multiply { param($x, $y) $result = $x * $y } Multiply 3 5
Attempts:
2 left
💡 Hint
Think about what PowerShell functions output by default.
✗ Incorrect
PowerShell functions output whatever is written to the pipeline. Since $result is assigned but not output or returned, the function returns nothing.
🚀 Application
advanced2:00remaining
What is the output of this PowerShell function with default parameter?
Given this function and call:
function Greet {
param($name = 'Friend')
"Hello, $name!"
}
GreetPowerShell
function Greet { param($name = 'Friend') "Hello, $name!" } Greet
Attempts:
2 left
💡 Hint
What happens if you call a function without giving a value for a parameter with a default?
✗ Incorrect
The parameter 'name' has a default value 'Friend'. Calling Greet without arguments uses this default, so it outputs 'Hello, Friend!'.
🧠 Conceptual
expert2:00remaining
What is the value of $result after running this PowerShell script?
Analyze this script:
function Test-Output {
param($val)
if ($val -gt 10) { return 'High' }
'Low'
}
$result = Test-Output 5PowerShell
function Test-Output { param($val) if ($val -gt 10) { return 'High' } 'Low' } $result = Test-Output 5 $result
Attempts:
2 left
💡 Hint
Consider what happens when the if condition is false and the function reaches the last line.
✗ Incorrect
If $val is not greater than 10, the function does not return inside the if block, so it outputs the last line 'Low'.