100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Variables and Data Types

How PowerShell stores data in loosely-typed variables, the common .NET types you'll use daily, and how to cast and inspect types explicitly.

FoundationsBeginner7 min readJul 10, 2026
Analogies

Variables and Data Types

PowerShell variables are prefixed with a dollar sign, like $score, and are dynamically (loosely) typed: you assign a value with $score = 250 without declaring a type up front, and PowerShell infers it. Under the hood, every variable actually holds a real .NET object - an integer is a System.Int32, a string is a System.String - so the dynamic-looking assignment is really strong typing with automatic inference.

🏏

Cricket analogy: Declaring $score = 250 in PowerShell without stating a type is like a scorer jotting down a total on the board without first declaring what format the match is - the runtime, like an experienced scorer, infers from context that it's a number and treats it accordingly.

Common Data Types

The types you'll use daily are strings ('hello' or "hello $name"), integers and doubles (42, 3.14), booleans ($true / $false), arrays created with @() for ordered collections, and hashtables created with @{} for key-value pairs like @{Name='Alice'; Age=30}. Double-quoted strings support variable interpolation and expression expansion, while single-quoted strings are always literal.

🏏

Cricket analogy: A hashtable @{Runs=250; Wickets=4} mirrors a match scorecard's key-value pairing of stat names to values, while an array @('India','Australia','England') is like a fixed batting order list - both are core PowerShell data types the way runs and wickets are core scorecard fields.

Type Casting and Inspection

You can force a value to a specific type with a type cast in square brackets, like [int]"42" to turn text into a number or [string]42 to turn a number into text. To check what type a variable actually holds, call its .GetType() method, e.g. (Get-Date).GetType().Name returns DateTime. You can also strictly type a variable at declaration with [int]$count = 5, which will throw an error if you later try to assign it something that can't convert to an integer.

🏏

Cricket analogy: Casting [int]"42" to force text into a number is like a third umpire overruling the on-field call with hard technology - .GetType() is the replay review confirming exactly what type of decision, an integer or a string, you're actually holding.

Set-StrictMode -Version Latest tightens PowerShell's normally forgiving behavior: it errors on references to uninitialized variables, non-existent object properties, and other silent-failure patterns that are easy to miss in loosely-typed code. It's a good habit to add near the top of scripts you intend to maintain long-term.

Arrays and Hashtables in Practice

Arrays are zero-indexed, so $fruits = @('Apple','Banana','Cherry') gives $fruits[0] as 'Apple' and $fruits[-1] as the last item, 'Cherry'. Hashtables are accessed by key rather than position, so $person = @{Name='Alice'; Age=30}; $person['Name'] or $person.Name both return 'Alice'. Both types support iteration with foreach ($item in $fruits) or, for hashtables, foreach ($key in $person.Keys).

🏏

Cricket analogy: Indexing $battingOrder[0] to get the opener is like reading position one on a printed team sheet, while $scorecard['Runs'] pulling a value by key from a hashtable is like looking up a specific player's tally by name on the scoreboard rather than by slot number.

PowerShell's -eq comparison can silently coerce types: "5" -eq 5 returns $true because the string is converted to match the number's type. This is convenient but can hide bugs when comparing user input against expected values - when exact type matching matters, cast explicitly first, e.g. ([int]$userInput -eq 5), or compare .GetType() results.

powershell
# Arrays and hashtables
$fruits = @('Apple', 'Banana', 'Cherry')
$fruits[0]        # Apple
$fruits[-1]       # Cherry

$person = @{ Name = 'Alice'; Age = 30 }
$person['Name']   # Alice
$person.Age       # 30

# Explicit casting and type inspection
[int]"42" + 8       # 50
(Get-Date).GetType().Name   # DateTime
[int]$strictCount = 5
  • Variables are prefixed with $ and are dynamically typed, inferred from the assigned value.
  • Every variable actually holds a real .NET object, e.g. System.Int32 or System.String.
  • Common types: strings, integers, doubles, booleans, arrays @(), and hashtables @{}.
  • Type casts like [int] and [string] force a value into a specific type explicitly.
  • .GetType() reveals the actual .NET type a variable currently holds.
  • Arrays are zero-indexed; hashtables are accessed by key, not position.
  • -eq can silently coerce types across strings and numbers; cast explicitly when exactness matters.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PowerShellStudyNotes#VariablesAndDataTypes#Variables#Data#Types#Common#StudyNotes#SkillVeris#ExamPrep