liveBook · Manning

Hashtable

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

A hashtable is a key-value collection that associates unique keys with values.

A hashtable groups related values under named keys. In PowerShell automation, it is a practical way to build structured input for commands, retain field values returned by another system, or define reusable parameter sets before invoking a command. section 9.3.1 section 11.3.3

Use a hashtable when named values need to travel together:

  • Build an API request body by assigning parameter names such as apiKey and ipAddress to their values, then pass the hashtable to Invoke-RestMethod with -Body. section 9.3.1
  • Read values returned as fields by indexing with a string key, such as $item['Title'] or $item['Template']. section 11.3.3
  • Collect command settings in a named structure, including nested values, before splatting them into a cmdlet. section 11.3.3
  • Represent expected test data for each case so a foreach loop can run one test block across multiple environments. section 14.1.3

Send API parameters as a hashtable

Create a body with the API’s required parameter names, then pass it intact to the request. This keeps credentials and lookup input explicit instead of assembling them into a URL or positional argument list. $Body = @{ apiKey = $apiKey ipAddress = $IP.ip } $geoData = Invoke-RestMethod -Uri $ApiUrl -Body $Body section 9.3.1

Hashtables also support incremental data shaping. Select-Object can receive a hashtable with Label and Expression keys to create a calculated property while processing JSON-derived objects. Default values can be added in bulk, then adjusted where exceptions are needed. section 6.2.3

$apiKey = "your_API_key"
$ApiUrl = "https:/ /geo.ipify.org/api/v2/country,city"
$Body = @{
    apiKey    = $apiKey
    ipAddress = $IP.ip
}
$geoData = $null
$geoData = Invoke-RestMethod -Uri $ApiUrl -Body $Body
$geoData.location
country    : US
region     : Illinois
city       : Chicago
lat        : 41.94756
lng        : -87.65650
postalCode : 60613
timezone   : -05:00

Inspect a complete request body and the returned location data to see named API parameters assembled and passed together. section 9.3.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Demonstrates construction, square-bracket lookup, calculated properties, nesting, and command splatting without detailing hashtable internals.
Examples
Uses geolocation request parameters, SharePoint request fields and connection settings, JSON property additions, and Linux test cases.
What you can do after
Apply it via PowerShell cmdlets to pass named parameters, retrieve keyed fields, and parameterize repeated tests.
How it connects
Connects API requests, Invoke-RestMethod, Select-Object, JSON, SharePoint list items, Pester, and foreach loops.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.