0
0
PowerShellscripting~15 mins

Function definition in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Function definition
📖 Scenario: You are automating a simple task to greet users by name. Instead of writing the greeting multiple times, you want to create a reusable function that prints a greeting message.
🎯 Goal: Create a PowerShell function called Say-Hello that takes a single parameter Name and prints Hello, Name! when called.
📋 What You'll Learn
Create a function named Say-Hello
The function must accept one parameter called Name
The function should print the greeting message Hello, Name! using the parameter
Call the function with the argument World to display Hello, World!
💡 Why This Matters
🌍 Real World
Functions help automate repetitive tasks by packaging code into reusable blocks. This saves time and reduces errors.
💼 Career
Knowing how to write and use functions is essential for scripting jobs, system administration, and automation roles.
Progress0 / 4 steps
1
Create the function skeleton
Write a PowerShell function named Say-Hello with no parameters yet. Just create the function block with empty braces.
PowerShell
Need a hint?

Use the function keyword followed by the function name and curly braces.

2
Add a parameter to the function
Inside the Say-Hello function, add a parameter block with one parameter named Name.
PowerShell
Need a hint?

Use param( [string]$Name ) inside the function to declare the parameter.

3
Add the greeting message inside the function
Inside the Say-Hello function, add a line that prints Hello, Name! using the $Name parameter with Write-Host and string interpolation.
PowerShell
Need a hint?

Use Write-Host "Hello, $Name!" to print the greeting.

4
Call the function to display the greeting
Call the Say-Hello function with the argument World to print Hello, World!.
PowerShell
Need a hint?

Simply write Say-Hello World to call the function with World.