📖 Scenario: You are creating a simple PowerShell script to greet users. Sometimes users provide their name, and sometimes they don't. You want the script to say "Hello, Guest!" when no name is given.
🎯 Goal: Build a PowerShell function that uses a default parameter value to greet a user by name or greet "Guest" if no name is provided.
📋 What You'll Learn
Create a function named Greet-User with a parameter Name that has a default value of 'Guest'.
Inside the function, output the greeting: Hello, <Name>! using the parameter value.
Call the function twice: once without any argument and once with the argument 'Alice'.
Print the output of both calls.
💡 Why This Matters
🌍 Real World
Scripts often need to handle missing information gracefully. Using default parameter values lets scripts run smoothly without requiring all inputs every time.
💼 Career
Knowing how to use default parameters is essential for writing flexible automation scripts, which is a common task in IT, DevOps, and system administration roles.
Progress0 / 4 steps
1
Create the function with a default parameter
Write a PowerShell function named Greet-User with a parameter Name that has the default value 'Guest'.
PowerShell
Hint
Use the param block inside the function to define parameters with default values.
2
Add greeting output inside the function
Inside the Greet-User function, add a line to output the greeting Hello, <Name>! using the parameter Name.
PowerShell
Hint
Use Write-Output with a double-quoted string and the $Name variable inside.
3
Call the function with and without arguments
Call the Greet-User function twice: once without any argument and once with the argument 'Alice'.
PowerShell
Hint
Call the function by its name alone for default, and with -Name 'Alice' for a custom name.
4
Print the greetings from the function calls
Run the script so it prints the greetings from both calls to Greet-User. The output should show Hello, Guest! and Hello, Alice! on separate lines.
PowerShell
Hint
Just run the script as is. The function calls already print the greetings.
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?