Arrays
Cheat sheet · https://pcornel.dk/topic.php?topic=3043
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 versionGet-Command <name>— which cmdlet/function/exe runsGet-Help <cmdlet> -Full— syntax and parametersGet-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
| Item | Meaning | Evidence boundary |
|---|---|---|
| Scalar | Single object | may not expose intended collection semantics |
| Array | Ordered collection | snapshot cardinality only |
| Pipeline enumeration | Element streaming | can flatten intended collection boundary |
| Index | Position lookup | depends on current ordering/count |
| Mutable collection | Efficient add/remove model | implementation 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