0
0
PowerShellscripting~20 mins

Why functions organize scripts in PowerShell - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use functions in PowerShell scripts?
Which of the following is the main reason to use functions to organize PowerShell scripts?
ATo prevent the script from running on certain computers
BTo make the script run faster by skipping unnecessary code
CTo group related commands for easier reuse and understanding
DTo avoid writing comments in the script
Attempts:
2 left
💡 Hint
Think about how functions help when you want to use the same code multiple times.
💻 Command Output
intermediate
1: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'
AHello, Alice!
BGreet -name Alice
CHello, $name!
DError: Missing argument for parameter 'name'
Attempts:
2 left
💡 Hint
Look at how the function uses the parameter to create the greeting.
🔧 Debug
advanced
2: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
AError: Missing argument for parameter 'b'
BOutput: 5
COutput: 10
DError: Unexpected token '+'
Attempts:
2 left
💡 Hint
Check how many arguments the function expects versus how many are given.
🚀 Application
advanced
2: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?
Afunction RunProcess { Write-Host "Starting process" Start-Sleep -Seconds 2 Write-Host "Process complete" }
Bfunction RunProcess() { Write-Host "Starting process"; Start-Sleep -Seconds 2; Write-Host "Process complete" }
Cfunction RunProcess { Write-Host "Starting process"; Start-Sleep 2; Write-Host "Process complete" }
Dfunction RunProcess { Write-Host "Starting process"; Start-Sleep -Seconds 2; Write-Host "Process complete" }
Attempts:
2 left
💡 Hint
Check the syntax for defining functions and calling commands with parameters.
🧠 Conceptual
expert
2:30remaining
Why does using functions improve script maintenance?
Which statement best explains why organizing scripts with functions improves maintenance?
AFunctions make scripts run faster by compiling code before execution.
BFunctions allow you to change code in one place and have it affect all uses, reducing errors and effort.
CFunctions prevent other users from reading your script by hiding code.
DFunctions automatically fix bugs in your script without manual changes.
Attempts:
2 left
💡 Hint
Think about how changing one function affects the whole script.