A variable in PowerShell is a named container that stores data or information you want to use later in your script.
Click to reveal answer
beginner
Why do we use variables in scripts?
We use variables to save data so we can reuse it, change it, or refer to it easily without typing the data again and again.
Click to reveal answer
beginner
How do you create a variable in PowerShell?
You create a variable by using the $ sign followed by the variable name, like $name = "Alice".
Click to reveal answer
beginner
What kind of data can variables store?
Variables can store many types of data like numbers, text, lists, or even complex objects.
Click to reveal answer
beginner
What happens if you change the value of a variable?
The variable updates to hold the new data, replacing the old value. This helps scripts adapt and work with changing information.
Click to reveal answer
In PowerShell, what symbol starts a variable name?
A$
B#
C@
D%
✗ Incorrect
Variables in PowerShell always start with the $ symbol.
Why do we store data in variables?
ATo make the script slower
BTo reuse and change data easily
CTo delete data automatically
DTo avoid using data
✗ Incorrect
Variables let us reuse and update data without retyping it.
Which of these can NOT be stored in a PowerShell variable?
ALists
BNumbers
CText
DCommands
✗ Incorrect
Variables store data like text, numbers, and lists, but commands are run, not stored as data.
What happens when you assign a new value to a variable?
AThe variable duplicates
BThe variable disappears
CThe old value is replaced
DThe script stops
✗ Incorrect
Assigning a new value replaces the old value in the variable.
Which is a correct way to create a variable named 'age' with value 30?
A$age = 30
Bage = $30
C#age = 30
D$30 = age
✗ Incorrect
Variables start with $ and are assigned values with =, like $age = 30.
Explain in your own words why variables are important in scripting.
Think about how you remember things to use later.
You got /4 concepts.
Describe how you create and update a variable in PowerShell.
Remember the $ symbol and the equals sign.
You got /3 concepts.
Practice
(1/5)
1. Why do we use variables in PowerShell scripts?
easy
A. To change the color of the text output
B. To make the script run faster
C. To delete files automatically
D. To store data so the script can use it later
Solution
Step 1: Understand the purpose of variables
Variables hold information so scripts can remember and reuse it.
Step 2: Compare options with variable purpose
Only storing data for later use matches the role of variables.
Final Answer:
To store data so the script can use it later -> Option D
Quick Check:
Variables store data = C [OK]
Hint: Variables remember data for scripts to reuse [OK]
Common Mistakes:
Thinking variables speed up scripts
Confusing variables with text formatting
Assuming variables delete files
2. Which is the correct way to create a variable named count with value 5 in PowerShell?
easy
A. $count = 5
B. count = 5
C. var count = 5
D. set count = 5
Solution
Step 1: Recall PowerShell variable syntax
Variables start with a $ sign, e.g., $count.
Step 2: Match syntax to options
Only $count = 5 uses $count = 5, which is correct in PowerShell.
Final Answer:
$count = 5 -> Option A
Quick Check:
Variables start with $ = B [OK]
Hint: Variables always start with $ in PowerShell [OK]
Common Mistakes:
Omitting the $ sign before variable name
Using other language syntax like var or set
Assigning without an equals sign
3. What will be the output of this PowerShell script?
$name = "Anna"
Write-Output $name
medium
A. $name
B. Write-Output $name
C. Anna
D. Error: Variable not defined
Solution
Step 1: Understand variable assignment
$name is assigned the string "Anna".
Step 2: Understand Write-Output behavior
Write-Output prints the value stored in $name, which is "Anna".
Final Answer:
Anna -> Option C
Quick Check:
Variable value printed = A [OK]
Hint: Write-Output prints variable's stored value [OK]
Common Mistakes:
Expecting variable name printed instead of value
Confusing output with command text
Thinking it causes an error
4. Identify the error in this PowerShell script:
$age = 30
Write-Output age
medium
A. Variable $age cannot store numbers
B. Missing $ before variable name in Write-Output
C. Write-Output cannot print variables
D. Assignment operator = is incorrect
Solution
Step 1: Check variable usage in Write-Output
Variables must have $ prefix when used, so "age" is incorrect.
Step 2: Verify other parts
$age = 30 is valid; Write-Output can print variables; = is correct.
Final Answer:
Missing $ before variable name in Write-Output -> Option B
Quick Check:
Variables need $ prefix = A [OK]
Hint: Always use $ before variable names when referencing [OK]
Common Mistakes:
Forgetting $ before variable in commands
Thinking variables can't hold numbers
Assuming Write-Output can't print variables
5. You want to store a user's name and age in variables and then print a message like "John is 25 years old." Which script correctly uses variables to do this?
hard
A. $userName = "John"
$userAge = 25
Write-Output "$userName is $userAge years old."
B. userName = "John"
userAge = 25
Write-Output "userName is userAge years old."
C. $userName = John
$userAge = "25"
Write-Output "$userName is $userAge years old."
D. $userName = "John"
$userAge = 25
Write-Output '$userName is $userAge years old.'
Solution
Step 1: Check variable assignment correctness
$userName = "John"
$userAge = 25
Write-Output "$userName is $userAge years old." assigns strings and numbers correctly with $ and quotes.
Step 2: Check output formatting
$userName = "John"
$userAge = 25
Write-Output "$userName is $userAge years old." uses double quotes allowing variable values to appear in output.
Step 3: Identify errors in other options
One option uses single quotes which do not expand variables; another misses $ signs and prints variable names literally; another misses quotes around John causing a syntax error.
Final Answer:
$userName = "John"
$userAge = 25
Write-Output "$userName is $userAge years old." -> Option A
Quick Check:
Use $ and double quotes for variable output = D [OK]
Hint: Use $ and double quotes to show variable values in strings [OK]
Common Mistakes:
Forgetting $ before variable names
Using single quotes which don't expand variables
Not quoting string values properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.