Challenge - 5 Problems
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why use functions in PowerShell scripts?
Which of the following is the main reason to use functions to organize PowerShell scripts?
Attempts:
2 left
💡 Hint
Think about how functions help when you want to use the same code multiple times.
✗ Incorrect
Functions group related commands so you can reuse them easily and keep your script organized and easier to read.
💻 Command Output
intermediate1:30remaining
Output of a simple PowerShell function call
What is the output of this PowerShell script?
PowerShell
function Greet { param($name) "Hello, $name!" } Greet -name 'Alice'
Attempts:
2 left
💡 Hint
Look at how the function uses the parameter to create the greeting.
✗ Incorrect
The function Greet takes a parameter 'name' and returns a string with that name inserted. Calling it with 'Alice' outputs 'Hello, Alice!'.
🔧 Debug
advanced2:00remaining
Identify the error in this PowerShell function
What error will this script produce when run?
PowerShell
function AddNumbers { param($a, [Parameter(Mandatory=$true)]$b) return $a + $b } AddNumbers 5
Attempts:
2 left
💡 Hint
Check how many arguments the function expects versus how many are given.
✗ Incorrect
The function expects two parameters but only one is provided, so PowerShell raises an error about the missing argument.
🚀 Application
advanced2:00remaining
Refactor repeated code into a function
You have this repeated code in your script:
Write-Host "Starting process"
Start-Sleep -Seconds 2
Write-Host "Process complete"
Which function correctly refactors this repeated code for reuse?
Attempts:
2 left
💡 Hint
Check the syntax for defining functions and calling commands with parameters.
✗ Incorrect
Option D correctly defines a function with commands separated by semicolons and uses the correct Start-Sleep syntax with '-Seconds'.
🧠 Conceptual
expert2:30remaining
Why does using functions improve script maintenance?
Which statement best explains why organizing scripts with functions improves maintenance?
Attempts:
2 left
💡 Hint
Think about how changing one function affects the whole script.
✗ Incorrect
Functions centralize code so updates happen in one place, making scripts easier to fix and improve without repeating changes everywhere.