Step 7. Variables and Data Types — Putting Values into Labeled Boxes

Step 7. Variables and Data Types — Putting Values into Labeled Boxes

Level 0 — Understanding How to Operate a Computer and How It’s Structured | Difficulty ★★☆☆☆ | Estimated time: 2–3 hours

Prerequisites: Step 6 complete. We’ll work in Windows PowerShell. No internet connection needed.

  • What you need: a Windows PC, PowerShell.
  • Safety: today is all experiments played inside memory, so it’s 100% safe.
  • Caution: let me warn you about today’s biggest trap in advance. Double quotes and single quotes behave differently in PowerShell. Beginners lose hours over this one thing. You’ll learn it head-on in section 3-4 and move on.

Today "programming" begins. But don’t be intimidated. What you’ll learn today is "giving a value a name so you can pull it back out later" — the same thing, in essence, as jotting a note in daily life. Everything in today’s chapter must be typed by hand. Reading code with your eyes alone will never make it stick. Mistakes are fine. Error messages are part of today’s textbook too.


1. Learning Objectives

By the end of this chapter, you can:

  • Store values in variables and pull them back out
  • Distinguish numbers, strings, and arrays, and check a data type with GetType()
  • Explain the difference between double quotes (variable expansion) and single quotes (verbatim)
  • Store a command’s entire result in a variable, count it, and pull out items

2. Background Knowledge — Today’s Tools and Concepts

Today’s Tools at a Glance

Category Details
Language/environment PowerShell 5.1 — full-fledged programming concepts start today
Today’s syntax $variable = value (store), $variable (retrieve), @(...) (array), $( ) (embedding inside a sentence)
Today’s tool GetType() (check data type)
Concepts you need variables and memory, data types (number/string/array), double quotes vs single quotes

Until now we’ve typed commands, seen results, and been done. Type Get-Date and a date appears, but the result passes across the screen and vanishes. A person would jot it in a notebook — storing a value under a name, like "wifi password = blue sky 77." In programming, the thing playing this notebook role is a variable, and the kind of thing written down — number, text, or list — is a data type. These two concepts are the alphabet of every script to come.

2-1. Variables — Labeled Boxes

A variable is "a box with a name tag that holds a value."

$name = "Lee"

What this one line means: "make a box with the name tag name, and put the text Lee inside it." Later, when you call $name, the contents of the box pop out.

In PowerShell, variable names always start with $. That dollar sign is the signal that "this is a variable."

2-2. Inside the Computer — Memory

Let’s move the box analogy a little closer to reality. The moment you run $name = "Lee", the computer writes "Lee" somewhere in memory (RAM) and remembers the pairing of the name name with that location. Close the PowerShell window and this record in memory disappears too — a variable is "a memo valid only while the window is open."

(Note: this word "memory" comes back in great depth in Level 1. The C language and buffer overflow attacks are all memory stories. For now, just remember one line: "variables are stored in memory.")

2-3. Data Types — The Kind of Thing in the Box

What you can do depends on what’s inside the box.

Data type Examples What you can do
Number (integer) 3, 42 arithmetic
Text (string) "Lee", "hello" join, slice, search
List (array) @("a","b","c") line up in order, pull out one at a time

A "string" is the term for "characters strung together in a row." Just think of it as text data. Anything wrapped in quotes is a string"3" is not the number 3 but the character "3". This difference actually causes a problem in section 3-3.


3. Follow Along

3-1. First Variable — Store and Retrieve

$name = "Lee"

Looks like nothing happened, right? That’s normal. Store commands are quiet. Now let’s pull it out:

$name
Lee

How to read the output: when you type just $name, PowerShell understands "show me the box’s contents" and prints them.

Why do this: these two moves — "store → retrieve" — are the basic gait of every script. With variables, you can use the same value a hundred times while managing it through one name tag, and when the value changes you fix it in just one place.

3-2. Number Variables — Becoming a Calculator

$a = 3
$b = 4
$a + $b
7

How to read the output: if 7 comes right out, it worked. We pulled the numbers out of two boxes and added them.

Why do this: once numbers live in variables, you can store a calculation result and use it in the next calculation. Analysis like "total log lines minus error lines equals normal lines" becomes possible.

For fun, try $a * $b (multiply) and $a / $b (divide) too. PowerShell becomes a calculator on the spot.

3-3. Numbers and Text Are Different — Data Types Made Real

This time let’s cause an accident on purpose. Type:

$x = "3"
$y = "4"
$x + $y

Predict first: will the result be 7, or not?

Actual result (verified):

34

Not 7 but 34! Because they were wrapped in quotes, "3" and "4" are not numbers but text, and + on text means "join together." This is the moment a data type actually changes a result.

Let’s check these boxes’ identities:

$x.GetType().Name
String

The computer itself answers: a string. Do the same with $a from before and it says Int32 (integer).

Why do this: "I clearly put in numbers but the math is weird" is a classic beginner bug. Nine times out of ten the cause is "I thought it was a number, but it was a string." Think of GetType() as an ID checker that reveals a value’s identity when you’re suspicious.

3-4. Today’s Biggest Trap — Double Quotes vs Single Quotes

Sometimes you want to use a variable inside a string. Let’s compare the two ways side by side. Type:

"Name: $name"
'Name: $name'

Output:

Name: Lee
Name: $name

How to read the output: in the first line (double quotes), the variable expanded and Lee went in; in the second line (single quotes), $name came out verbatim, as literal text.

Why the difference: double quotes "inspect the contents and swap any variables for their values" (this is called variable expansion). Single quotes "treat the contents exactly as written."

When to use which: if you want to embed a value inside a sentence, double quotes. If you want the $ symbol itself as a character, single quotes. Start with double quotes by default, and if you think "huh? the variable didn’t expand," suspect the quotes first.

Predict first: how would "My age: $a + $b years old" print? (Answer: My age: 3 + 4 years old$a and $b each expand, but + is treated as plain text. No calculation happens inside quotes. Check it yourself.)

3-5. Arrays — Many Things in One Box

You want to handle several names. You could make three boxes, but one list box is better:

$list = @("apple", "banana", "kiwi")

@(...) is the sign saying "this is a list." To pull things out, you use a number tag:

$list[0]
apple

How to read the output: the point is that apple, not banana, came out. Computers count from 0. Number 0 is the first, number 1 is the second. Try $list[1] and $list[2] too.

Why do this: lists come up constantly in real work — lists of log files, users, servers. And when they meet next chapter’s loops, they become the powerful pattern "do the same task for every item in the list."

Let’s also learn right now how to count a list:

$list.Count
3

.Count means "count them for me." Similar in role to the Measure-Object you saw in Step 5.

3-6. Putting It Together — Handling a List of Names

Let’s gather what we learned today:

$members = @("Kim", "Lee", "Park")
$members.Count
"First member: $($members[0])"
3
First member: Kim

One new notation: "$($members[0])" — to put one slot of an array inside double quotes, you have to wrap it once in $( ). If you just write "$members[0]", the actual result is this:

Kim Lee Park[0]

The entire array expanded and then [0] got stuck on as literal text — PowerShell got confused about how far the variable extended. "Inside double quotes, wrap anything complicated in $( )" — just remember that.

Why do this: the combination "list + count + embed in a sentence" becomes the standard pattern for auto-generating report sentences later. Example: "Files scanned: $($files.Count)".

3-7. Real-World Feel — Storing a Command’s Result in a Variable

So far we’ve written values in directly ($a = 3). But a variable’s real power shows when you store a command’s entire result:

$now = Get-Date
$now
2026년 9월 9일 수요일 오전 8:31:07

(On English Windows this prints something like Wednesday, September 9, 2026 8:31:07 AM.)

What just happened: instead of printing to the screen, Get-Date‘s result (the current time) went into a box called $now. Now you can reuse this value as much as you want.

One step further — let’s combine it with a command from Step 5:

$services = Get-Service
$services.Count
310

(The number differs per computer. On the author’s PC it came out 310.)

How to read the output: the computer’s entire service list went into an array called $services, and .Count counted it. The command result became an array — individual services can also be pulled out by number, like $services[0].

Why do this: this pattern — "store the result in a variable → count it → pull out what you need" — is the skeleton of every analysis script to come. Read a log file into a variable, filter only errors into another variable, judge by the count. When it meets Step 8’s loops, the picture completes.

Predict first: right after running $services = Get-Service and then 10 minutes later — would the two .Count values be the same? (Answer: usually yes, but if a service started or stopped in the meantime, it can differ. A variable is a snapshot of the moment it was stored — and that instinct is the starting point of the security mindset of "compare the record against the present.")


4. Missions & Exercises

Mission — Member Introduction Script

Write code of 5 lines or fewer that satisfies the requirements below:

  1. Create an array $team holding three names (including yours — any names)
  2. Check the count of $team
  3. Using a double-quoted string, print "Our team has N members" (N comes automatically from the variable)
  4. Print "The team lead is OOO" (use the first slot of the array)

Exercises

Exercise 1. Explain the result of "3" + "4" and why.

Exercise 2. Explain the output difference between "Name: $name" and 'Name: $name'.

Exercise 3. You want to put one slot of an array inside double quotes. Instead of "$members[0]", how should you write it?

Exercise 4. You ran $services = Get-Service, then closed the window and reopened it. What happens to $services.Count? Why?


5. Model Answers & Completion Criteria

Mission Model Answer

$team = @("Lee", "Kim", "Park")
$team.Count
"Our team has $($team.Count) members"
"The team lead is $($team[0])"

Expected output:

3
Our team has 3 members
The team lead is Lee

How to verify: if an actual number appears in the N slot of the third output, success. If $team.Count prints as literal text, it’s a quote problem (review 3-4). If a weird sentence appears in the fourth, it’s a $( ) problem (review 3-6).

Deeper dive: what happens if you print $team[3]? (Hint: numbering starts at 0, and the list only has slots 0, 1, and 2. Try it yourself — in actual testing, it quietly prints nothing. "A nonexistent slot isn’t an error, it’s an empty value" — that’s a PowerShell personality trait worth learning now.)

Exercise Answers

Answer 1. The result is 34. Wrapped in quotes, they’re not numbers but strings, and + on strings is not addition but "joining." (To add them as numbers: 3 + 4 without quotes.)

Answer 2. Double quotes expand variables — they swap the $name inside for its value (e.g., Name: Lee). Single quotes take it verbatim — they print the literal text Name: $name.

Answer 3. "$($members[0])" — you must wrap it in $( ). Written plainly, the whole array expands and [0] sticks on as text, producing weird output (see the actual test in 3-6).

Answer 4. It becomes empty (or errors). Variables are stored only in memory and disappear when the PowerShell window closes. This is exactly why Step 9 "saves to a script file" — a file survives even when the window closes.

Completion Checklist

  • [ ] I can store a value in a variable and pull it back out to print
  • [ ] I can explain the difference between double and single quotes
  • [ ] I can explain why "3" + "4" is 34
  • [ ] I can check a variable’s data type with GetType()
  • [ ] I can make an array and pull out items by number (counting from 0)
  • [ ] I can store a command result in a variable and count it with .Count
  • [ ] Mission: I completed the member introduction script

6. Common Pitfalls & Fixes

Wall 1. I typed a variable and nothing came out

Symptom: typed $name = "Lee" and no output at all.
Cause: it’s not broken. Store commands are quiet by nature.
Fix: type $name once more to check the contents. Understand it as "typed and quiet = stored."

Wall 2. The variable prints literally inside a sentence

Symptom: "Name: $name" prints as Name: $name.
Cause: you’re using single quotes ' '. Single quotes treat the contents verbatim.
Fix: switch to double quotes " ". This trap is a rite of passage every beginner goes through once.

Wall 3. Numbers joined instead of adding

Symptom: expected 3 + 4 to be 7 but got 34.
Cause: the values were wrapped in quotes and became strings. + on strings is joining.
Fix: store them without quotes, like $a = 3. When in doubt, verify identity with $a.GetType().Name — it must say Int32 to be a number.

Wall 4. The wrong thing comes out of an array

Symptom: wanted the first item but got the second, or called past the end and got nothing.
Cause: you forgot numbering starts at 0. The first is [0]; the end of a three-item list is [2].
Fix: memorize "number = which one − 1." And build the habit of checking the list length with $list.Count first.

Wall 5. I made a variable and it vanished a while later

Symptom: I definitely stored it, but an empty value comes out.
Cause: you closed and reopened the PowerShell window, or made it in a different window. Variables live only in memory, separately per window.
Fix: keep practicing in the same window. Close the window and all variables are gone.


7. Summary

Today’s Core Table

Concept Analogy PowerShell notation
Variable a labeled box (stored in memory) $name = "Lee"
String text data "hello" (quotes required)
Integer number data 3 (no quotes)
Array a list box with number tags @("a","b","c"), starting at [0]

Today’s Trap Card

Situation Result Lesson
"$name" (double quotes) variable expands when embedding a value
'$name' (single quotes) literal text when you want the $ symbol itself
"3" + "4" 34 quotes = string
3 + 4 7 no quotes = number
"$list[0]" weird output array slots go in $($list[0])

Instincts That Matter More Than Commands

"Always be aware of what kind of thing is in the box." "I thought it was a string but it was a number (or vice versa)" is a bug that sometimes becomes the seed of a real security vulnerability. When in doubt, check identity with GetType() — that habit is today’s real takeaway.

Two nice-to-knows: code that "takes user-input values into variables and embeds them straight into commands" is a staple ingredient of injection attacks — you’ll feel the reason for the iron rule "never blindly trust values from outside" when you carry out such attacks yourself in Level 2. And in real work, people use names like $loginFailCount instead of $a — "names that tell you what they are when read." Code whose purpose is visible from names alone reads without a manual.


Once every box is checked, Step 7 is complete. Click the checkbox in the sidebar to save your progress.