Pcornel
Windows · PowerShell

Arrays

Cheat sheet · https://pcornel.dk/topic.php?topic=3043

Back to the Topic

Answer

PowerShell arrays are ordered collections, but PowerShell can automatically unwrap/enumerate collections through pipelines and collapse zero/one/many results differently depending on context. This makes collection shape an important automation contract.

Remember

  • One result can behave differently from an explicitly preserved one-element array.
  • Pipelines enumerate many collection types by default.
  • The unary comma can create/preserve an array boundary in specific expressions.
  • Fixed PowerShell arrays are easy to use but repeated += historically implied new-array construction; choose collection types based on workload/version behavior.
  • Count/Length must be interpreted against the actual object/collection shape.

First checks

  • $PSVersionTable — PowerShell edition and version
  • Get-Command <name> — which cmdlet/function/exe runs
  • Get-Help <cmdlet> -Full — syntax and parameters
  • Get-ExecutionPolicy -List — execution policy per scope

Verify in this order

items collected → array object → indexed or enumerated in the pipeline

Where it usually breaks

First mismatch in definition/configuration → security context → service/task/process creation → runtime state → output/side effect → event/performance evidence; preserve identity/native error/pre-state before mutation.

PowerShell → Arrays reference map

ItemMeaningEvidence boundary
ScalarSingle objectmay not expose intended collection semantics
ArrayOrdered collectionsnapshot cardinality only
Pipeline enumerationElement streamingcan flatten intended collection boundary
IndexPosition lookupdepends on current ordering/count
Mutable collectionEfficient add/remove modelimplementation choice affects behavior/performance

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=3043

Confirmation

Confirm action

Continue?