PowerShell Basics Level: Beginner Chapter 3: Think in Objects, Not Text
Variables
A variable is a named box that holds a value. PowerShell variables start with a dollar sign, and they can hold text, numbers, dates, or whole objects.
Lesson 1 of 7 in Chapter 3: Think in Objects, Not Text. Previous step in this path: Getting help: Get-Help, About, and IntelliSense. 3 learning objectives, 6 examples, and a knowledge check.
Start: what you will learn
A variable is a named box that holds a value. PowerShell variables start with a dollar sign, and they can hold text, numbers, dates, or whole objects.
Learning objectives
- Create and read a variable
- Tell system variables apart from your own variables
- Combine variables into a new value
Concept in simple words
What it is: A variable is a named place that holds a value, and in PowerShell every variable name starts with a dollar sign, such as $name. You create it by storing a value in it, and you read it back by typing its name. A variable can hold text, a number, a true or false value, a date, or a whole object, such as one service or one process.
Why it works
Why it exists: Without variables, you would have to retype every value, and you could not reuse a result. A variable lets you capture a result once, give it a clear name, and use it again in later commands. That is why variables sit at the start of almost every useful script, even a short one.
Why it works this way: PowerShell does not ask you to declare, which means state in advance, what type of value a variable will hold. It works out the type from the value you assign, which is the value you store with the equals sign, and the type then decides what you can do with that value, such as adding numbers or joining text. If you store a new value of a different type in the same variable later, the type changes to match the new value.
Think of it like this
Think of it like this: A variable is like a labelled box. The label is the name, the contents are the value, and the kind of contents matters, because you can add two boxes of numbers, but two boxes of text join together instead of adding up. A box of text and a box of numbers do not behave the same way, even if they look similar on screen.
How it works, step by step
How it works, step by step: First, create a variable with the equals sign, for example $name = "Izaz". The equals sign here means store, not compare. Then read the variable back by typing its name on its own, for example $name, and PowerShell shows the stored value. Nothing is saved to a file by this step, the value lives in your current session, which is your current PowerShell window, until you close it or remove the variable.
How it works, step by step, continued: Next, you can use the variable inside a double quoted string, such as "Hello, $name", and PowerShell replaces the name with its value, which is called expansion. In a single quoted string, PowerShell does not expand the variable, so you see the literal text $name instead. That one quote difference catches many beginners, so check your quotes when a value does not appear.
How it works, step by step, continued: When you need more than a plain name inside a string, use a subexpression, which is $() around an expression. For example, "Year: $($today.Year)" first reads the Year property from $today, and then puts that result into the string. A plain $today.Year inside double quotes does not work the same way, because PowerShell stops reading the variable name at the dot, so use $() whenever a property or a method must appear inside a string.
What the type changes: Try $a = 4 and $b = 6, and $a + $b gives 10, because both values are numbers. Try $a = "4" and $b = "6", and $a + $b gives 46, because both values are text, and plus joins text. The left hand value matters most here, because PowerShell follows the type on the left when it decides what plus means. So always check whether a value is in quotes before you predict what plus will do.
How to combine values: You have three clean ways to combine values. First, use plus for numbers and for joining text. Second, use double quoted expansion, such as "Hello, $name", for a readable sentence. Third, use -join to turn a list into one text value, which you will meet in the arrays lesson. Pick the way that reads most clearly, and keep the result in a new variable when you will use it again, such as $greeting = "Hello, $name".
How to compare, not store: The equals sign = stores a value, while -eq compares two values and returns true or false. So $a = 4 stores 4 in $a, while $a -eq 4 asks whether $a already holds 4. Mixing these two up is a classic beginner error, because $a = 4 inside a test stores a value instead of asking a question, so read = as store and -eq as equals every time.
How to update a value: You can store a new value in a variable you already created, and the old value is replaced. A common pattern is counting, such as $count = 0, then $count = $count + 1, which reads the old value, adds 1, and stores the new value back in the same variable. You can also write $count += 1, which is a shorter way to say add 1 to the current value and store it back.
How to reuse a result: A variable can hold the output of a command, not only text you type. For example, $today = Get-Date stores a date object, and $today.Year then reads the year from that object. This is powerful, because you are storing the whole object, with all its properties, not only the text you saw on screen. Later commands can then read any property they need from the stored object.
What automatic variables are: PowerShell creates some variables itself, and these are called automatic variables. Examples are $PSVersionTable, which holds version details, $HOME, which holds your home folder path, $PWD, which holds your current folder, $_, which holds the current object in a pipeline, and $?, which holds true if the last command succeeded and false if it failed. These variables describe the state of your session, so read them, but do not overwrite them.
Why it works
Why you must not overwrite automatic variables: PowerShell depends on automatic variables to track what it is doing. Microsoft Learn describes most of them as read only in concept, and says they should not be written to, even where writing is technically possible. If you reuse a name like $input or $_ for your own value, later commands may behave strangely, and the strange result may appear far from the line that caused it. Use your own clear names, such as $serverName or $userCount, and leave automatic names alone.
How to inspect variables: Use Get-Variable to list variables, and Get-Variable -Name PSVersionTable to inspect one variable in detail. This is a safe, read only way to see what PowerShell already provides, and it helps you check whether a name is already taken before you reuse it. If you created a variable by mistake, Remove-Variable removes your own variable from the session, but do not use it on automatic variables.
How naming works: Choose a name that says what the value means, such as $firstName rather than $x, because you will read your own code later and need to understand it quickly. Variable names can contain letters, digits, and underscores, and PowerShell treats variable names as case insensitive, so $name and $Name refer to the same variable. Keep names short enough to type, and clear enough to understand at a glance.
What happens when you assign one variable to another: When you write $b = $a, $b receives the value that $a holds at that moment. For simple values like numbers and text, $b then has its own copy, so changing $b does not change $a. For objects, both names can point to the same object, so changing a property through one name can show up through the other name. The safe habit is to treat a second variable as a second label for now, and to re-read the value after any change, rather than assuming you made a separate copy.
Common mistakes, in plain words: The most common mistake is using single quotes when you wanted expansion, so the dollar sign prints literally. The next is storing a number in quotes, and then wondering why plus joins text. Another is using = when you meant -eq, so you store a value instead of testing it. A fourth is reusing an automatic name, and then blaming a later command for the confusion. A fifth is assuming a variable survives after you close the window, when in fact it lives only in that session unless you save your commands in a file and run them again.
How to spot a variable problem: If a value prints as $name instead of the value itself, check your quotes first. If a sum looks like joined digits, such as 46 instead of 10, check whether either value is in quotes. If a test always seems true, check whether you wrote = instead of -eq. If a command behaves oddly after you set a variable, check whether you overwrote an automatic name, and try a fresh session with a different name.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
$name = "value"Example 1
A double quoted string expands the variable. A single quoted string would print the dollar sign literally.
$firstName = "Izaz"
Write-Output "Hello, $firstName"Illustrative output: Hello, Izaz
Example 2
Shows one system variable and its value, so you can inspect what PowerShell already provides.
Get-Variable -Name PSVersionTableIllustrative output: Name Value ---- ----- PSVersionTable @{PSVersion=5.1}
Example 3
The first two lines store numbers, not text, because the values have no quotes. The third line reads both variables and adds them, because the plus sign adds numbers. If the values had been in quotes, they would have been strings, and plus would have joined them into the text 46 instead.
$a = 4
$b = 6
$a + $bIllustrative output: 10
Example 4
The first line stores the text Izaz. The second line uses double quotes, so $name is replaced by Izaz. The third line uses single quotes, so PowerShell keeps the text literal and prints the dollar sign and the name exactly as typed. Compare the two outputs line by line to see the quote rule in action.
$name = "Izaz"
"Hello, $name"
'Hello, $name'Illustrative output: Hello, Izaz Hello, $name
Example 5
The first line stores a date object from Get-Date, not only text. The second line uses $() so the Year property is read first, and then its value is placed into the string. Without $(), PowerShell would stop at the dot and would not read the property correctly inside the string.
$today = Get-Date
"Year: $($today.Year)"Illustrative output: Year: 2026
Example 6
The first line stores 0. The second line reads the old value, adds 1, and stores the result back in the same variable, so $count now holds 1. The third line reads the variable back, so you can confirm the update worked. This read, change, store back pattern is how counters work.
$count = 0
$count = $count + 1
$countIllustrative output: 1
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Diagram
Choose a stage above to see what it does.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Using single quotes when you want a variable to expand. Single quotes keep text literal.
- Naming a variable $input or another automatic name, then wondering why PowerShell behaves strangely.
- Storing a number in quotes, such as $a = "4", and then being surprised when $a + $b joins text instead of adding numbers.
- Using = when you meant -eq, so you store a value instead of comparing two values.
- Assuming a variable will still exist after you close the PowerShell window. Variables live in the current session, so save your commands if you need them again.
- Reading $today.Year inside a double quoted string without $(), and then wondering why the property value does not appear correctly.
Knowledge check
Knowledge check
What does "Hello, $name" print if $name holds Izaz?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: A variable starts with a dollar sign, = stores a value, -eq compares values, double quotes expand a variable, single quotes keep text literal, $() lets a property appear inside a string, the type of the value decides what plus and other operators do, automatic variables describe your session and must not be overwritten, and a variable lives in your current session until you close it or remove it.
Next step: Arrays