Variables: `$name`
A variable is a name PowerShell keeps a value under, so $owl = "…" stores a long path once, $drawings = Get-ChildItem … keeps a whole list of files, and $owl hands the value back in any later command until you close the window.
Apprentice · PowerShell · Updated
After this lesson you can
- Store a value or a command's answer in a variable with $name = …, and use it in later commands
- Explain what a variable holds once it's set, a snapshot that changes only when you set it again, and why a mistyped name gives nothing
Key takeaways
$owl = '…'puts a value in a box called owl.$owl, read "the value of owl", hands it back in any later command.- A variable can hold a command's whole answer:
$drawings = Get-ChildItem …keeps the file objects, properties and all. - A variable is a snapshot: it doesn't change when the folder does, until you set it again.
- Names ignore capital letters. A name you never set is empty, not an error, so a typo quietly gives you nothing.
- Your variables last until you close the window.
$HOMEand$PSVersionTableare PowerShell's own, filled in for you.
You're working in a folder five levels deep, and you've typed its path so often your fingers know it, typos included. Or you listed a folder's files, and now you want to sort that same list a second way without asking for it again. Or a guide says "save the result of this command, you'll need it in a minute." Each time, you want PowerShell to remember something for you. That's what a variable is for, and every script in this course is built on them. A help-desk tech keeps a computer's name in one while fixing it; a web developer keeps a project's folder in one.
Set up the workshop
This chapter's practice folder is a workshop. Ada Wren, an inventor, keeps her machines there (they're tiny PowerShell scripts), and her newest project, a clockwork owl, sits four folders deep. Copy these lines into PowerShell. They print nothing when they work, and running them again is safe:
New-Item -ItemType Directory -Force -Path "$HOME\terminal-practice\workshop\tools", "$HOME\terminal-practice\workshop\projects\clockwork-owl\drawings" | Out-Null
Set-Location "$HOME\terminal-practice\workshop"
Set-Content letter.txt 'Back from the inventors'' fair on December 4. Keep the machines running! - Ada'
Set-Content kettle-bot.ps1 'if ($env:POWER -eq "on") { "Pouring tea..." } else { "NO POWER" }'
Set-Content tools\oil.ps1 '"Glug, glug. The gears are oiled and shining."'
Set-Content projects\clockwork-owl\drawings\wings.txt 'Wings: 12 brass feathers each, hinged at the shoulder'
Set-Content projects\clockwork-owl\drawings\beak.txt 'Beak: copper, opens on the hour'
Set-Content projects\clockwork-owl\drawings\gears.txt 'Gears: 7 teeth on the small one, 21 on the big one, 3 turns to 1'
A name for a value: `$owl = …`
Picture a box with a label on it. The label is the variable's name, and what you put inside is its value. In PowerShell every variable's name starts with $, and = puts a value in the box:
$owl = "$HOME\terminal-practice\workshop\projects\clockwork-owl"
$owl
C:\Users\Sam\terminal-practice\workshop\projects\clockwork-owl
Read the first line as "put this path in the box called owl". It printed nothing: PowerShell quietly kept the value. The second line is the variable on its own, and $owl is read "the value of owl", so PowerShell shows what's in the box. (The double quotes let $HOME fill in your home folder; the next lesson, Text: Quotes and $( ), shows how.)
Now $owl works anywhere you'd type the path. Set-Location $owl takes you there, and this lists the owl's drawings:
Get-ChildItem "$owl\drawings"
Directory: C:\Users\Sam\terminal-practice\workshop\projects\clockwork-owl\drawings
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/7/2026 10:15 AM 33 beak.txt
-a--- 11/7/2026 10:15 AM 66 gears.txt
-a--- 11/7/2026 10:15 AM 55 wings.txt
Your dates and times will be the moment you made the practice folder.
A whole answer in a box
A variable can hold a command's answer too. Here the box holds the three file objects themselves, with every property from What an Object Is: Get-Member, so you can count them and sort them without asking the disk again:
$drawings = Get-ChildItem "$owl\drawings"
$drawings.Count
$drawings | Sort-Object Length -Descending | Select-Object Name, Length
3
Name Length
---- ------
gears.txt 66
wings.txt 55
beak.txt 33
A variable is a photo, not a window. Add a drawing to the folder, and the box still holds the three files it got:
Set-Content "$owl\drawings\tail.txt" 'Tail: 9 brass feathers, fanned'
$drawings.Count
$drawings = Get-ChildItem "$owl\drawings"
$drawings.Count
3
4
Only when you set the variable again does it see the fourth file. Setting a variable replaces what was in the box; +=, read "add to", adds to what's there. $cups = 0, then $cups += 1 twice, leaves 2 in $cups.
Names and typos
Names ignore capital letters: $OWL and $owl are the same box. Use letters, digits and underscores; a dash looks like the start of a parameter, so PowerShell stops:
$gear-size = 7
ParserError:
Line |
1 | $gear-size = 7
| ~~~~~
| Unexpected token '-size' in expression or statement.
$gearSize, with a capital letter where each new word starts, is the usual way to write a name of two words.
A mistyped name is a quieter problem. A box you never filled is empty, so $owel prints nothing at all, with no error to warn you. When a command does something strange, check your variable's spelling first.
PowerShell's own variables
Some boxes are filled before you type anything. $HOME is your home folder, $PSVersionTable says which PowerShell you're running, and $_ is "this item" from Filtering: Where-Object and Comparisons. They're automatic variables, so don't use their names for your own. Get-Variable shows a box's name and its value, without the $:
Get-Variable owl
Name Value
---- -----
owl C:\Users\Sam\terminal-practice\workshop\projects\clockwork-owl
Your own variables last until you close the window. A new window starts with empty boxes; Your Profile, in the next chapter, shows how to fill some every time PowerShell starts. Everything here works the same in Windows PowerShell 5.1 and on a Mac or Linux, where paths use /.
Try it. Put your Downloads folder in a variable with $dl = "$HOME\Downloads", then count the files in it with (Get-ChildItem $dl -File).Count.
Check yourself
2 questions to try on the lesson page.
So what
A variable lets PowerShell remember for you: a path you'd rather not retype, or a whole command's answer to sort and count again and again. Scripts are built from them, starting in this chapter.