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)
✗ Incorrect
In PowerShell, default values are assigned using the equals sign (=) inside the param block.
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.
✗ Incorrect
If a parameter has a default value, PowerShell uses it when no argument is provided.
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.
✗ Incorrect
Default values let users call functions without specifying every parameter.
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)
✗ Incorrect
Only the equals sign (=) is valid for default values in PowerShell parameters.
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.
✗ Incorrect
Providing an argument overrides the default value.
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.
Practice
(1/5)
1. What is the main purpose of using default parameter values in a PowerShell function?
easy
A. To make the function run faster
B. To force the user to always provide a value
C. To provide fallback values when no argument is passed
D. To disable the function if parameters are missing
Solution
Step 1: Understand default parameter values
Default parameter values are set to be used when no argument is provided by the user.
Step 2: Identify the purpose
This helps the function run smoothly without errors by using fallback values.
Final Answer:
To provide fallback values when no argument is passed -> Option C
Quick Check:
Default values = fallback values [OK]
Hint: Defaults fill in missing inputs automatically [OK]
Common Mistakes:
Thinking defaults speed up execution
Believing defaults force user input
Assuming defaults disable functions
2. Which of the following is the correct syntax to set a default value of 10 for a parameter named $count in a PowerShell function?
PowerShell uses the equals sign (=) to assign default values in parameters.
Step 2: Match syntax to options
Only param([int]$count = 10) uses = correctly to assign 10 as default.
Final Answer:
param([int]$count = 10) -> Option B
Quick Check:
Default assignment uses = in PowerShell [OK]
Hint: Use = to assign default values in param block [OK]
Common Mistakes:
Using : or => instead of = for defaults
Confusing syntax with other languages
Placing default outside param block
3. What will be the output of this PowerShell function call?
function Greet { param([string]$name = "Guest") "Hello, $name!" } Greet
medium
A. Hello, Guest!
B. Hello, !
C. Hello, $name!
D. Error: Missing argument
Solution
Step 1: Analyze function parameter default
The parameter $name has a default value "Guest" if no argument is passed.
Step 2: Check function call without argument
Calling Greet without arguments uses the default "Guest".
Final Answer:
Hello, Guest! -> Option A
Quick Check:
Default used when no argument given = Hello, Guest! [OK]
Hint: No argument? Default value is used [OK]
Common Mistakes:
Expecting empty string output
Thinking variable name prints literally
Assuming error on missing argument
4. Identify the error in this PowerShell function definition:
function Show-Info { param([string]$info =) Write-Output $info }
medium
A. Missing default value after =
B. Parameter type [string] is invalid
C. Write-Output cannot be used inside functions
D. Function name cannot contain a hyphen
Solution
Step 1: Check parameter default syntax
The parameter $info has an equals sign but no value after it, which is invalid.
Step 2: Validate other parts
Parameter type [string] is valid, Write-Output is allowed, and hyphens are allowed in function names.
Final Answer:
Missing default value after = -> Option A
Quick Check:
Default value must follow = [OK]
Hint: Default values must not be empty after = [OK]
Common Mistakes:
Leaving = without a value
Misunderstanding function name rules
Thinking Write-Output is disallowed
5. You want a PowerShell function Calculate-Sum that adds two numbers, but if the second number is not provided, it should default to the first number. Which parameter default setup achieves this?