📖 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
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
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
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
Hint
Simply write Say-Hello World to call the function with World.
Practice
(1/5)
1. What is the main purpose of defining a function in PowerShell?
easy
A. To delete files automatically
B. To group commands so they can be reused easily
C. To run commands only once
D. To create a new variable
Solution
Step 1: Understand what functions do
Functions group multiple commands into one block that can be run many times.
Step 2: Compare options with function purpose
Only To group commands so they can be reused easily describes grouping commands for reuse, which is the function's purpose.
Final Answer:
To group commands so they can be reused easily -> Option B
Quick Check:
Functions = reusable command groups [OK]
Hint: Functions group commands for reuse, not variables or deletion [OK]
Common Mistakes:
Confusing functions with variables
Thinking functions run commands only once
Assuming functions delete files automatically
2. Which of the following is the correct way to define a function named Greet in PowerShell?
easy
A. def Greet() { Write-Output 'Hello' }
B. function:Greet Write-Output 'Hello'
C. func Greet { Write-Output 'Hello' }
D. function Greet { Write-Output 'Hello' }
Solution
Step 1: Recall PowerShell function syntax
PowerShell functions start with the keyword 'function', followed by the name and braces containing commands.
Step 2: Check each option
function Greet { Write-Output 'Hello' } uses correct syntax: 'function Greet { ... }'. Others use incorrect keywords or missing braces.
Final Answer:
function Greet { Write-Output 'Hello' } -> Option D
Quick Check:
PowerShell functions start with 'function' keyword [OK]
Hint: PowerShell functions start with 'function' keyword and braces [OK]
Common Mistakes:
Using 'def' like Python
Using 'func' instead of 'function'
Missing braces around function body
3. What will be the output of this PowerShell script?
The function takes two parameters $a and $b, adds them, and returns the sum.
Step 2: Call the function with 3 and 5
Calling Add-Numbers 3 5 passes 3 to $a and 5 to $b, so return is 3 + 5 = 8.
Final Answer:
8 -> Option C
Quick Check:
3 + 5 = 8 [OK]
Hint: Parameters add numbers; 3 + 5 equals 8 [OK]
Common Mistakes:
Concatenating numbers as strings (35)
Returning parameters without adding
Expecting parentheses like other languages
4. Identify the error in this PowerShell function definition:
function SayHello {
param($name)
Write-Output "Hello, $name"
}
medium
A. No error, function is correct
B. param block should be inside braces
C. Missing parentheses after function name
D. Write-Output cannot use double quotes
Solution
Step 1: Review PowerShell function syntax
Functions use 'function Name { param(...) ... }' syntax; parentheses after name are optional.
Step 2: Check param block and Write-Output usage
Param block is correctly inside braces; double quotes allow variable expansion in Write-Output.
Final Answer:
No error, function is correct -> Option A
Quick Check:
Function syntax is valid as written [OK]
Hint: Param block inside braces is correct; parentheses after name optional [OK]
Common Mistakes:
Expecting parentheses after function name
Thinking param must be outside braces
Believing double quotes disallowed in Write-Output
5. You want to create a PowerShell function Get-Square that takes a number and returns its square. Which function definition correctly achieves this?
hard
A. function Get-Square { param($num) return $num * $num }
B. function Get-Square($num) { $num ^ 2 }
C. function Get-Square { param($num) $num ** 2 }
D. function Get-Square { param($num) return $num ^ 2 }
Solution
Step 1: Understand PowerShell multiplication and function syntax
PowerShell uses * for multiplication; param block defines input parameters.
Step 2: Evaluate each option
function Get-Square { param($num) return $num * $num } correctly uses param and multiplies $num by itself. function Get-Square($num) { $num ^ 2 } uses valid param syntax but ^ is bitwise XOR, not multiplication. function Get-Square { param($num) $num ** 2 } uses ** which is not PowerShell operator. function Get-Square { param($num) return $num ^ 2 } uses ^ which is bitwise XOR, not power.
Final Answer:
function Get-Square { param($num) return $num * $num } -> Option A
Quick Check:
Use * for multiplication, param inside braces [OK]
Hint: Use * for multiplication; param inside braces defines inputs [OK]
Common Mistakes:
Using ^ for power instead of multiplication
Using ** which is invalid in PowerShell
Incorrect param syntax with parentheses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.