📖 Scenario: You are creating a simple script to greet users by their name and show their age. This is like writing a friendly note that changes based on who you are talking to.
🎯 Goal: Build a PowerShell script that stores a name and age, then uses string interpolation to create a greeting message that includes both values.
📋 What You'll Learn
Create variables with exact names and values
Use string interpolation with double quotes
Print the final greeting message
💡 Why This Matters
🌍 Real World
Scripts often need to create messages that change based on user data, like greeting users or showing status updates.
💼 Career
Understanding string interpolation helps automate reports, notifications, and user interactions in many IT and scripting jobs.
Progress0 / 4 steps
1
Create variables for name and age
Create a variable called $name and set it to the string "Alice". Create another variable called $age and set it to the number 30.
PowerShell
Hint
Use = to assign values. Strings need double quotes.
2
Create a greeting message using string interpolation
Create a variable called $greeting and set it to a string that says Hello, my name is Alice and I am 30 years old. Use string interpolation with $name and $age inside double quotes.
PowerShell
Hint
Use double quotes and include variables directly inside the string.
3
Print the greeting message
Use the Write-Output command to print the variable $greeting.
PowerShell
Hint
Use Write-Output followed by the variable name to print.
4
Run the script and see the output
Run the script to display the greeting message exactly as: Hello, my name is Alice and I am 30 years old.
PowerShell
Hint
Make sure you run the script and check the output matches exactly.
Practice
(1/5)
1. Which type of quotes in PowerShell allows variable values to be inserted inside a string using interpolation?
easy
A. No quotes
B. Single quotes (')
C. Backticks (`)
D. Double quotes (")
Solution
Step 1: Understand string quoting in PowerShell
PowerShell uses double quotes to allow variable interpolation inside strings.
Step 2: Compare single vs double quotes
Single quotes treat the content literally, so variables are not replaced by their values.
Final Answer:
Double quotes (") -> Option D
Quick Check:
Double quotes allow interpolation = D [OK]
Hint: Use double quotes to insert variables inside strings [OK]
Common Mistakes:
Using single quotes expecting variable replacement
Confusing backticks with quotes
Thinking no quotes allow interpolation
2. Which of the following is the correct syntax to interpolate the variable $name inside a string in PowerShell?
easy
A. "Hello, $name!"
B. 'Hello, $name!'
C. `Hello, $name!`
D. Hello, $name!
Solution
Step 1: Identify correct string syntax for interpolation
Double quotes are needed to allow variable interpolation in PowerShell strings.
Step 2: Check each option
"Hello, $name!" uses double quotes correctly; 'Hello, $name!' uses single quotes (no interpolation); `Hello, $name!` uses backticks (escape character, not string quotes); Hello, $name! is missing quotes.
Final Answer:
"Hello, $name!" -> Option A
Quick Check:
Double quotes for interpolation = A [OK]
Hint: Remember: double quotes for variables inside strings [OK]
Common Mistakes:
Using single quotes expecting variable to expand
Using backticks as string delimiters
Omitting quotes around strings
3. What will be the output of the following PowerShell code?
$user is defined correctly with single quotes, so no error there.
Step 2: Analyze string interpolation
The $message string uses single quotes, so $user is not replaced by its value but printed literally.
Final Answer:
Single quotes prevent variable interpolation -> Option B
Quick Check:
Single quotes block interpolation = B [OK]
Hint: Use double quotes to enable variable replacement [OK]
Common Mistakes:
Thinking single quotes allow interpolation
Expecting semicolons in PowerShell
Assuming Write-Output can't print variables
5. You want to create a greeting message that includes a user's name stored in $userName and their age stored in $age. Which of the following PowerShell commands correctly uses string interpolation to produce: Hello, Alice! You are 30 years old.
hard
A. $greeting = "Hello, $userName! You are $age years old."
B. $greeting = 'Hello, $userName! You are $age years old.'
C. $greeting = "Hello, $userName! You are ${age} years old."
D. $greeting = "Hello, ${userName}! You are ${age} years old."
Solution
Step 1: Understand variable interpolation with complex variables
Variables inside strings can be interpolated directly or with braces ${} to clarify boundaries.
Step 2: Evaluate each option
$greeting = "Hello, $userName! You are $age years old." works correctly and is the most common usage; $greeting = 'Hello, $userName! You are $age years old.' uses single quotes (no interpolation); $greeting = "Hello, $userName! You are ${age} years old." uses braces for $age which is valid but unnecessary here; $greeting = "Hello, ${userName}! You are ${age} years old." uses braces for both variables which is valid but less common.
Final Answer:
$greeting = "Hello, $userName! You are $age years old." -> Option A
Quick Check:
Direct interpolation without braces is common and correct = A [OK]
Hint: Use ${} to clearly separate variables in strings when needed [OK]
Common Mistakes:
Using single quotes expecting interpolation
Not using braces when variable names are ambiguous
Mixing variable syntax incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.