liveBook · Manning

Collection

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A collection is a container that groups multiple values as a single data object.

A collection is a container that groups multiple values or objects as one data object. Picture it as an in-memory table: each object is a row, and its properties are columns. section 8.1

Store several computer names

PowerShell creates a collection from a comma-separated list: $computers = 'SRV-02','SERVER1','localhost'. Displaying $computers shows three separate string objects, one per line. Keep commas outside the quotation marks; placing them inside creates one string containing commas. section 16.4

Collections support several practical operations:

  • Access a property across the collection: $processes.Name. PowerShell implicitly unrolls the collection and retrieves the Name property from each process; ForEach-Object and Select-Object -ExpandProperty provide equivalent forms. section 16.4.4
  • Test membership with -contains or -in. For example, $collection -contains 'abc' checks whether the string exists, while 'abc' -in $collection reverses the operand order. section 26.2.4
  • Iterate through every item with foreach; its script block runs once for each object in the collection. section 23.1.1
$processes = Get-Process
$processes.Name 

Run $processes = Get-Process followed by $processes.Name to observe property unrolling across a collection of process objects. section 16.4.4

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Defines collections, demonstrates storage and implicit property enumeration, then briefly applies membership tests and iteration.
Examples
Uses computer-name strings, process objects, text files, membership tests, and a number array from one through ten.
What it assumes
Builds from variables containing single simple values and introduces objects, properties, and methods as collection-related terminology.
What you can do after
Apply it via PowerShell by creating collections, reading shared properties, testing membership, and iterating through items.
How it connects
Connects collections with objects, properties, methods, variables, arrays, pipelines, and foreach loops.

Also explained in

Compare how 16 books explain collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.