0
0
PowerShellscripting~20 mins

Default parameter values in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Default Parameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell function call?

Consider this PowerShell function with a default parameter value:

function Greet($name = 'Guest') { Write-Output "Hello, $name!" }

What will be the output of Greet when called without arguments?

PowerShell
function Greet($name = 'Guest') { Write-Output "Hello, $name!" }
Greet
AError: Missing argument for parameter 'name'
BHello, Guest!
CHello, $name!
DHello, !
Attempts:
2 left
💡 Hint

Think about what happens when you call a function without providing a value for a parameter that has a default.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a parameter with a default value in PowerShell?

Choose the correct syntax to define a function parameter $count with a default value of 5.

Aparam([int]$count <- 5)
Bparam([int]$count:5)
Cparam([int]$count => 5)
Dparam([int]$count = 5)
Attempts:
2 left
💡 Hint

PowerShell uses the equals sign = to assign default values to parameters.

🔧 Debug
advanced
2:00remaining
Why does this PowerShell function throw an error when called without arguments?

Look at this function:

function Show-Info {
  param(
    [Parameter(Mandatory=$true)]
    [string]$name,
    [int]$age = 30
  )
  Write-Output "$name is $age years old."
}

Show-Info

Why does calling Show-Info without arguments cause an error?

PowerShell
function Show-Info {
  param(
    [Parameter(Mandatory=$true)]
    [string]$name,
    [int]$age = 30
  )
  Write-Output "$name is $age years old."
}

Show-Info
ABecause $name has no default value and is required, so calling without it causes an error.
BBecause $age has a default value, but $name must be an integer.
CBecause the function syntax is invalid; default values must be last parameters.
DBecause Write-Output cannot handle variables without explicit values.
Attempts:
2 left
💡 Hint

Check which parameters have default values and which do not.

🚀 Application
advanced
2:00remaining
What is the output of this script using default parameter values?

Given this PowerShell script:

function Calculate-Sum {
  param(
    [int]$a = 10,
    [int]$b = 20
  )
  return $a + $b
}

$result1 = Calculate-Sum
$result2 = Calculate-Sum -a 5
$result3 = Calculate-Sum -b 15
$result4 = Calculate-Sum -a 3 -b 7

Write-Output "$result1,$result2,$result3,$result4"

What will be the output?

PowerShell
function Calculate-Sum {
  param(
    [int]$a = 10,
    [int]$b = 20
  )
  return $a + $b
}

$result1 = Calculate-Sum
$result2 = Calculate-Sum -a 5
$result3 = Calculate-Sum -b 15
$result4 = Calculate-Sum -a 3 -b 7

Write-Output "$result1,$result2,$result3,$result4"
A30,25,25,10
B30,25,35,10
C30,25,30,10
D30,15,25,10
Attempts:
2 left
💡 Hint

Remember that parameters with default values use those defaults if not provided.

🧠 Conceptual
expert
2:00remaining
How does PowerShell handle default parameter values when using splatting?

Consider this function:

function Show-Message {
  param(
    [string]$text = 'Hello',
    [string]$color = 'Blue'
  )
  Write-Output "$text in $color"
}

And this splatting call:

$params = @{ text = 'Hi' }
Show-Message @params

What will be the output and why?

PowerShell
function Show-Message {
  param(
    [string]$text = 'Hello',
    [string]$color = 'Blue'
  )
  Write-Output "$text in $color"
}

$params = @{ text = 'Hi' }
Show-Message @params
AHello in Blue - because splatting ignores default values.
BHi in - because $color is empty when not passed in splatting.
CHi in Blue - because $color uses its default value when not provided in splatting.
DError - because splatting requires all parameters to be specified.
Attempts:
2 left
💡 Hint

Think about how splatting passes only specified parameters and how defaults fill in missing ones.