0
0
PowerShellscripting~20 mins

Parameters in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Parameters in PowerShell Scripts
📖 Scenario: You are creating a PowerShell script to greet users by their name and optionally their age. This script will use parameters to accept input values when run.
🎯 Goal: Build a PowerShell script that uses parameters to accept a Name and an optional Age. The script will greet the user by name and mention their age if provided.
📋 What You'll Learn
Create a parameter block with a mandatory Name parameter of type string
Add an optional Age parameter of type int with a default value
Use the parameters inside the script to print a greeting message
Print the greeting message showing the name and age if provided
💡 Why This Matters
🌍 Real World
Scripts with parameters let users provide input values when running them, making scripts flexible and reusable for different situations.
💼 Career
Knowing how to use parameters is essential for automation tasks, system administration, and writing professional PowerShell scripts that others can use easily.
Progress0 / 4 steps
1
Create the Parameter Block with Name
Write a PowerShell script that defines a parameter block with a mandatory string parameter called Name.
PowerShell
Need a hint?

Use param() to define parameters. Mark Name as mandatory with [Parameter(Mandatory=$true)].

2
Add an Optional Age Parameter
Add an optional integer parameter called Age to the parameter block with a default value of 0.
PowerShell
Need a hint?

Define Age as an integer with a default value by writing [int]$Age = 0.

3
Write the Greeting Message Using Parameters
Write code that creates a variable message which says "Hello, Name!" if Age is 0, or "Hello, Name! You are Age years old." if Age is greater than 0.
PowerShell
Need a hint?

Use an if statement to check if Age is greater than 0 and set message accordingly.

4
Print the Greeting Message
Write a line to print the message variable to the console.
PowerShell
Need a hint?

Use Write-Output to print the message to the console.