0
0
PowerShellscripting~15 mins

Default parameter values in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Default parameter values
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Just run the script as is. The function calls already print the greetings.