Variables, Arrays, Hash Tables, and Scriptblocks in PowerShell

Variables are the foundation of data handling in PowerShell. They store values—numbers, strings, objects, and more—allowing scripts to reuse information without recalculating it. Building on variables, PowerShell provides richer structures such as arrays, hash tables, and scriptblocks, enabling powerful automation, data manipulation, and dynamic execution. Understanding these constructs is essential for writing clean, efficient, and scalable PowerShell scripts

PowerShell VariablesArraysHash TablesScriptblocks.Data StructuresAutomation

~2 min read • Updated Dec 21, 2025

1. Variable Basics


Variables in PowerShell act as containers for temporary data. They begin with $ and can store any type of object.

Naming Rules

  • Use letters, numbers, and underscores.
  • $var = "Value" assigns data.
  • $ is not part of the name—omit it when referencing variable names in parameters like -ErrorVariable var.
  • Use curly braces for complex names: ${valid name!} = 123.

Types & Coercion

PowerShell dynamically converts types:

$a = 5; $b = "5"
$a + $b   # 10 (numeric addition)
$b + $a   # "55" (string concatenation)

Explicit typing prevents errors:

[int]$num = "Richard"   # Conversion error

Strict Mode

Set-StrictMode -Version Latest helps catch typos and uninitialized variables.

Built-in Variables

Explore with Get-Variable or Get-PSDrive Variable:. Manage using New-Variable, Set-Variable, Remove-Variable.


2. Arrays

Arrays store ordered collections of items (any type). Indexing starts at 0.

Create Arrays

$array = 1,2,3,"four"
$array = @('one','two','three')

Access Elements

$array[0]     # First element
$array[-1]    # Last element
$array.Count  # Number of items

Add / Remove Items

$array += "new"
$array = $array | Where-Object { $_ -ne "remove" }

3. Hash Tables

Hash tables store key-value pairs and are essential for structured data.

Create

$hash = @{
  Name = 'Don'
  Department = 'IT'
  City = 'Las Vegas'
}

Access & Manage

$hash.Name
$hash.Keys
$hash.Values
$hash.Add('Title','CTO')
$hash.Remove('City')
$hash.Count

Ordered Hash Tables (PowerShell v3+)

$ordered = [ordered]@{
  First = 1
  Second = 2
  Third = 3
}

Preserves insertion order—useful for PSCustomObject creation.

Common Uses

  • Custom properties in Select-Object.
  • Default parameters via $PSDefaultParameterValues.

4. Scriptblocks

Scriptblocks are reusable blocks of code enclosed in {}. They can accept parameters and be executed dynamically.

Create & Invoke

$block = { param($name) Get-Process -Name $name }
&$block svc*

Common Uses

  • Where-Object
  • ForEach-Object
  • Invoke-Command
  • Dynamic function-like behavior

Conclusion

Variables, arrays, hash tables, and scriptblocks form the core of PowerShell’s data-handling capabilities. Mastering these structures enables efficient automation, cleaner scripts, and more powerful solutions. Whether you're filtering data, building objects, or executing dynamic code, these constructs provide the flexibility and control needed for advanced PowerShell scripting.

Written & researched by Dr. Shahin Siami