liveBook · Manning

Array

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

An array is an ordered collection of values that can be accessed as a single sequence.

An array is an ordered collection of values. In PowerShell, comma-separated values automatically form an array, and a parameter such as String[] can accept one or many strings. section 3.5.4

Use arrays to work with several values at once:

  • Create one with a comma-separated list, such as five strings or the range 1..10. section 3.5.4 section 23.1.1
  • Access an individual element with its index, such as $array[0]. section 26.2.3
  • Iterate through every element with foreach, which runs its script block once per collection item. section 23.1.1
  • Convert between arrays and delimited strings with -join and -split. section 26.2.3

Split a tab-delimited record

$array = (gc computers.tdf) -split "`t"; $array[0] returns the first field, such as Server1. Use -join to combine array elements into a pipe-delimited string for reuse or output to a file. section 26.2.3

PS C:\> $array = "one","two","three","four","five"
PS C:\> $array
one
two
three
four
five

Run the supplied PowerShell example to create and display a five-element array before transforming it. section 26.2.3

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

How deep it goes
Defines creation, indexing, conversion, and iteration, while deferring more complex ways to supply lists.
Examples
Uses file paths, five strings, tab-delimited computer records, numeric ranges, and foreach output.
What you can do after
Apply it via PowerShell arrays, -join, -split, indexing, and foreach to process collections.
How it connects
Connects arrays with parameters, strings, files, indexing, and foreach loops.

Also explained in

Compare how 40 books explain array →

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.