liveBook · Manning

Map

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A hash table is a key-value data structure that associates unique keys with corresponding values.

A PowerShell hash table stores values under named keys. Use it when a script needs a compact collection of parameter values, output properties, or gradually assembled results. section 12.1 section 12.2

Common automation uses include:

  • Build an arguments hash table conditionally, such as including StartName only when a new username was supplied. section 10.5.3
  • Splat command parameters by passing the variable with @ instead of $, keeping long command calls readable. section 12.1
  • Construct output properties before creating an object, then send the object through the pipeline. section 12.2
  • Add properties as results become available, allowing success and failure paths to produce the same structured output. section 25.5.3

Splatting a CIM query

Create a parameter dictionary and pass it to the command with @params. Switch parameters receive $True as their value. $params = @{'ClassName'='Win32_OperatingSystem' 'ComputerName'='CLIENT1' 'Verbose'=$True} Get-CimInstance @params The same pattern can hold separate parameter sets for operating-system, computer-system, drive, and processor queries. section 12.1

Building structured output

Use keys as property names and values as collected data, then create an object for pipeline consumers. $props = @{'ComputerName'=$computer 'OSVersion'=$os.version 'RAM'=($cs.totalphysicalmemory / 1GB)} $obj = New-Object -TypeName PSObject -Property $props Write-Output $obj A normal hash table may display properties in an unexpected order. Use formatting or Select-Object when presentation order matters; an ordered hash table is possible but usually unnecessary. section 12.2 section 12.3

# Output data
$props = @{'ComputerName'=$computer
           'OSVersion'=$os.version
           'SPVersion'=$os.servicepackmajorversion
           'OSBuild'=$os.buildnumber
           'Manufacturer'=$cs.manufacturer
           'Model'=$cs.model
           'Procs'=$cs.numberofprocessors
           'Cores'=$cs.numberoflogicalprocessors
           'RAM'=($cs.totalphysicalmemory / 1GB)
           'Arch'=$proc.addresswidth
           'SysDriveFreeSpace'=$drive.freespace}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj

Inspect a complete property dictionary that turns computer information into reusable pipeline output. section 12.2

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Demonstrates literal construction, splatting, conditional keys, object creation, dynamic additions, and display-order tradeoffs.
Examples
Demonstrates CIM queries, machine-information objects, service arguments, and DNS results assembled from successful or failed lookups.
What it assumes
Assumes basic PowerShell commands and parameters while teaching hash-table construction, splatting syntax, and property mapping through examples.
What you can do after
Apply dictionaries to shorten commands, create reusable structured output, and add properties conditionally before pipeline emission.
How it connects
Connects dictionaries with splatting, command parameters, CIM, objects, properties, pipelines, and error handling.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.