Bird
Raised Fist0
PowerShellscripting~10 mins

Variable creation with $ in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Variable creation with $
Start script
↓
Use $ to create variable
↓
Assign value to variable
↓
Variable stores value
↓
Use variable later
↓
End script
This flow shows how PowerShell creates a variable using $ and assigns a value to it for later use.
Execution Sample
PowerShell
$name = "Alice"
Write-Output $name
This script creates a variable $name with value "Alice" and then prints it.
Execution Table
StepActionVariableValueOutput
1Create variable $name and assign "Alice"$nameAlice
2Output the value of $name$nameAliceAlice
3End of script$nameAlice
💡 Script ends after outputting the value of $name
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$nameundefinedAliceAliceAlice
Key Moments - 2 Insights
Why do we use $ before the variable name?
In PowerShell, $ tells the shell this is a variable. Without $, it is treated as plain text. See execution_table step 1 where $name is created.
What happens if we try to output the variable without $?
Without $, PowerShell prints the variable name as text, not its value. See execution_table step 2 where $name outputs "Alice".
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $name after step 1?
A"name"
Bundefined
CAlice
Dempty string
💡 Hint
Check the 'Value' column in row for step 1 in execution_table
At which step does the script output the value stored in $name?
AStep 2
BStep 3
CStep 1
DNo output in script
💡 Hint
Look at the 'Output' column in execution_table rows
If we remove $ from $name in Write-Output, what will be printed?
AAlice
Bname
C$name
DError
💡 Hint
Recall key_moments about using $ to access variable value
Concept Snapshot
PowerShell variables start with $.
Assign value with =, e.g. $var = "value".
Use $var to access the stored value.
Without $, text is treated literally.
Variables store data for later use.
Full Transcript
This lesson shows how to create variables in PowerShell using the $ symbol. The $ tells PowerShell that what follows is a variable name. We assign a value to the variable using =. For example, $name = "Alice" creates a variable named name with the value Alice. Later, we can use $name to get the value stored. The script outputs Alice when we write Write-Output $name. Without the $, PowerShell treats the word as plain text and does not output the variable's value. This is why $ is important to access variables. The execution table shows each step: creating the variable, outputting it, and ending the script. The variable tracker shows how $name changes from undefined to "Alice" and stays that way. Remember, always use $ before variable names in PowerShell.

Practice

(1/5)
1. What does the $ symbol do in PowerShell when placed before a word like $name?
easy
A. It prints the word 'name' to the screen.
B. It creates a variable named 'name' to store data.
C. It comments out the line.
D. It runs a command called 'name'.

Solution

  1. Step 1: Understand the role of $ in PowerShell

    The $ symbol is used to declare or access variables in PowerShell.
  2. Step 2: Identify what $name means

    $name means a variable named 'name' that can hold data like text or numbers.
  3. Final Answer:

    It creates a variable named 'name' to store data. -> Option B
  4. Quick Check:

    $ before word = variable creation [OK]
Hint: Remember: $ always starts a variable name [OK]
Common Mistakes:
  • Thinking $ runs commands
  • Confusing $ with comments
  • Assuming $ prints text directly
2. Which of the following is the correct way to create a variable named age with the value 30 in PowerShell?
easy
A. $age = 30
B. set age = 30
C. age = 30
D. var $age = 30

Solution

  1. Step 1: Recall PowerShell variable syntax

    Variables start with $ and assignment uses =.
  2. Step 2: Check each option

    $age = 30 uses $age = 30, which is correct syntax for variable creation and assignment.
  3. Final Answer:

    $age = 30 -> Option A
  4. Quick Check:

    Variable = $name = value [OK]
Hint: Always start variable names with $ in PowerShell [OK]
Common Mistakes:
  • Omitting the $ before variable name
  • Using 'set' keyword which is not PowerShell syntax
  • Adding 'var' keyword which is not used in PowerShell
3. What will be the output of this PowerShell code?
$greeting = "Hello"
$name = "Sam"
Write-Output "$greeting, $name!"
medium
A. Error: Variable not defined
B. $greeting, $name!
C. Hello $name!
D. Hello, Sam!

Solution

  1. Step 1: Understand variable assignment

    $greeting is assigned "Hello" and $name is assigned "Sam".
  2. Step 2: Understand string interpolation in double quotes

    Inside double quotes, variables like $greeting and $name are replaced by their values.
  3. Final Answer:

    Hello, Sam! -> Option D
  4. Quick Check:

    Variables inside "" show their values [OK]
Hint: Use double quotes to insert variables inside strings [OK]
Common Mistakes:
  • Using single quotes which do not expand variables
  • Expecting variable names to print literally
  • Forgetting to assign variables before use
4. Identify the error in this PowerShell script:
$number = 10
$number = $number + 5
Write-Output number
medium
A. Cannot add numbers in PowerShell
B. Variable $number not declared
C. Missing $ before 'number' in Write-Output
D. Write-Output cannot print variables

Solution

  1. Step 1: Check variable usage in Write-Output

    The script tries to print 'number' without $, so it prints the word literally.
  2. Step 2: Correct usage of variable in output

    To print the variable value, it should be Write-Output $number.
  3. Final Answer:

    Missing $ before 'number' in Write-Output -> Option C
  4. Quick Check:

    Variables need $ to be recognized [OK]
Hint: Always use $ before variable names when printing [OK]
Common Mistakes:
  • Forgetting $ before variable in output
  • Thinking variables need declaration keyword
  • Assuming Write-Output can't print variables
5. You want to create a variable $fullName by combining two variables $firstName and $lastName with a space in between. Which code correctly does this in PowerShell?
hard
A. $fullName = $firstName + " " + $lastName
B. $fullName = '$firstName $lastName'
C. $fullName = $firstName $lastName
D. $fullName = "$firstName$lastName"

Solution

  1. Step 1: Understand string concatenation in PowerShell

    Use + to join strings and include a space as " ".
  2. Step 2: Analyze each option

    $fullName = $firstName + " " + $lastName correctly adds $firstName, a space, and $lastName to form full name.
  3. Final Answer:

    $fullName = $firstName + " " + $lastName -> Option A
  4. Quick Check:

    Use + and " " to join variables with space [OK]
Hint: Use + and " " to join variables with space [OK]
Common Mistakes:
  • Using single quotes which do not expand variables
  • Missing operator between variables
  • Concatenating without space when needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard