Variables

By the end of this lesson you'll be able to store text, numbers, true/false values, arrays, and hashtables in PowerShell variables, convert between types with casting, and build clean output with string interpolation and here-strings — the foundation of every script you'll write.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

1. Declaring Variables & Common Types

To make a variable, pick a name starting with $ and assign a value with = : $name = "Alice" . You never declare the type — PowerShell is dynamically typed , so it figures the type out from the value. The everyday types are String (text), Int32 (whole numbers), Double (decimals) and Boolean ( $true / $false ). There's also $null , which means "no value at all". Read this worked example, run it, then you'll write your own.

2. Strings: Interpolation, Quotes & Here-Strings

The quote you choose changes what happens. Double quotes expand — a $variable inside the text is replaced by its value, and $(...) runs a small expression and drops the result in. Single quotes are literal — what you type is exactly what you get, so '$name' stays as the text $name . For multi-line text, a here-string ( @" ... "@ ) keeps your layout intact, and the double-quoted form still expands variables inside it.

Your turn. The script below is almost complete — fill in the three blanks marked ___ using the hints in the comments, then run it.

3. Collections: Arrays & Hashtables

When you need more than one value, reach for a collection. An array holds values in order — build it with @(...) and read items by position, where [0] is the first and [-1] is the last. A hashtable holds key = value pairs (a lookup table) — build it with @{' ... '} and read a value by its key with $h.Key or $h['Key'] . Inside a string, wrap property access in $(...) so it's evaluated.

4. Type Casting

Sometimes a value is the wrong type — most often you have text that needs to be a number . Put the target type in square brackets in front of the value to cast it: [int]$typed , [double]$priceText , [string]$age . This matters because PowerShell decides what + does from the left-hand value: a string on the left glues text together, while a number on the left adds. Cast first and the maths behaves.

Now you try. Input from a user always arrives as text, so before you can do maths you must cast it. Fill in the two blanks:

For large collections that grow in a loop, avoid $arr += item — it copies the whole array every time, which is O(n). Use a typed list instead: $list = [System.Collections.Generic.List[string]]::new() and $list.Add("x") , which is O(1). For ordered key/value data where insertion order matters, use [ordered]@{' ... '} — a plain hashtable does not guarantee order.

No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check your output against the example in the comments. This combines a hashtable, an array, casting a bool, and a here-string — exactly the kind of small script real automation is made of.

Practice quiz

What character must every PowerShell variable name start with?

  • $
  • @
  • #
  • %

Answer: $. Every variable name begins with $, for example $name or $count.

What type is the value 19.99 in PowerShell?

  • Int32
  • String
  • Boolean
  • Double

Answer: Double. A decimal number like 19.99 is a Double.

How do double-quoted strings differ from single-quoted ones?

  • They are slower
  • They cannot span lines
  • They expand variables and $() subexpressions
  • They must be empty

Answer: They expand variables and $() subexpressions. Double quotes expand variables and subexpressions; single quotes are literal.

What does 'Raw $user' (single quotes) print?

  • Raw Sam
  • An error
  • Raw $user
  • Raw

Answer: Raw $user. Single quotes are literal, so $user is not expanded.

How do you build an array in PowerShell?

Arrays are built with @( ), e.g. @(1, 2, 3).

Which index gets the LAST item of an array $a?

Negative indexing counts from the end, so $a[-1] is the last item.

What syntax builds a hashtable (key/value pairs)?

  • @( )

Hashtables use @{ }, e.g. @{ Name = 'Web'; IP = '10.0.0.1' }.

Why does the string "42" plus 8 give "428"?

  • 8 is invalid
  • The left value is a string, so + concatenates
  • PowerShell rounds up
  • Integers cannot be added

Answer: The left value is a string, so + concatenates. When the left operand is a string, + glues text together instead of adding.

How do you cast the text "42" to a whole number?

  • (int)"42"

Use the bracketed type cast [int]"42" to get a real Int32.

What is the recommended way to test whether $x is null?

  • $x -eq $null
  • $x == null
  • isnull $x
  • $null -eq $x

Answer: $null -eq $x. Put $null on the left ($null -eq $x) to avoid array-comparison surprises.