0
0
PowerShellscripting~5 mins

Default parameter values in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a default parameter value in PowerShell functions?
A default parameter value is a value assigned to a function parameter that is used if no argument is provided when the function is called. It helps avoid errors and makes parameters optional.
Click to reveal answer
beginner
How do you define a default value for a parameter in a PowerShell function?
You assign the default value directly in the parameter declaration using the equals sign, like: param([string]$name = "Guest").
Click to reveal answer
beginner
Why are default parameter values useful in scripting?
They make functions easier to use by allowing callers to skip parameters when the default is fine. This reduces the need to always specify every argument.
Click to reveal answer
beginner
What happens if you call a PowerShell function without providing a value for a parameter that has a default?
The function uses the default value assigned to that parameter automatically.
Click to reveal answer
beginner
Show a simple PowerShell function with a default parameter value and explain its output when called with and without an argument.
Example: function Greet { param([string]$name = "Friend") "Hello, $name!" } Calling Greet returns "Hello, Friend!" because no argument was given. Calling Greet -name "Anna" returns "Hello, Anna!" because the argument overrides the default.
Click to reveal answer
How do you assign a default value to a parameter in a PowerShell function?
Aparam([type]$param = value)
Bparam([type]$param: value)
Cparam([type]$param <- value)
Dparam([type]$param ? value)
What happens if you call a function without specifying a parameter that has a default value?
AThe function throws an error.
BThe function uses the default value for that parameter.
CThe function ignores the parameter.
DThe function waits for user input.
Why use default parameter values in scripts?
ATo make parameters mandatory.
BTo slow down the script.
CTo allow skipping parameters when defaults are fine.
DTo confuse users.
Which of these is a correct default parameter declaration in PowerShell?
Aparam([int]$count = 5)
Bparam([int]$count == 5)
Cparam([int]$count : 5)
Dparam([int]$count <- 5)
If a function parameter has a default value, can you still provide a different value when calling the function?
AOnly if the parameter is mandatory.
BNo, the default always overrides.
COnly if the default is null.
DYes, the provided value replaces the default.
Explain how to use default parameter values in a PowerShell function and why they are helpful.
Think about how to make parameters optional with a fallback value.
You got /3 concepts.
    Write a simple PowerShell function with a default parameter and describe what happens when you call it with and without that parameter.
    Use param block and assign a default value.
    You got /3 concepts.