0
0
PowerShellscripting~10 mins

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

Choose your learning style9 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.