Use this topic when a command sometimes returns a scalar and sometimes a collection, indexing behaves unexpectedly, pipeline enumeration changes nested data, or repeated array growth causes performance problems.
Arrays
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.
1. Create: $a = @(1,2,3); count: $a.Count.
2. Single result may not be an array → wrap in @().
3. Adding with += copies the array — use a List for large data.
4. Index from 0; $a[-1] last.
5. Loop: foreach ($i in $a) {}.
items collected → array object → indexed or enumerated in the pipeline
Add explicit validation/normalization at the function boundary and test edge cases.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
Do not write code that works only when two or more objects are returned. Do not assume .Count has identical meaning on arbitrary objects. Avoid fixing shape bugs by indiscriminately wrapping every value in arrays because that can create nested collections downstream.
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Test zero, one and many input/result cases. Inspect type and count before indexing. For nested data, pass representative values through the same pipeline/function boundary and show whether the collection is preserved or enumerated.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- 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.
$PSVersionTable— PowerShell edition and versionGet-Command <name>— which cmdlet/function/exe runsGet-Help <cmdlet> -Full— syntax and parametersGet-ExecutionPolicy -List— execution policy per scope
ReferenceWhat it is1 section · depth on demand
Core model
PowerShell is convenient around collections, but convenience can hide cardinality. A function emitting no objects, one object or multiple objects may be captured as $null, a scalar-like object or a collection depending on how it is called and wrapped. Scripts should define whether consumers expect exactly one object or a collection.
ReferenceImportant terms & boundaries2 sections · depth on demand
PowerShell boundary
Use @(...) when you need a predictable array wrapper around pipeline output. Be deliberate with nested arrays because pipeline enumeration can flatten one level of enumerable output. For frequent additions/removals, consider generic lists or command streaming instead of building large arrays before processing.
In depth
Arrays hold ordered collections. += creates a new array each time, which is slow in loops; use a list or collect loop output directly.
$servers = @('srv01','srv02')
$servers[0]; $servers[-1]; $servers.Count
$list = [System.Collections.Generic.List[string]]::new(); $list.Add('srv03')
$results = foreach ($s in $servers) { Test-Connection $s -Count 1 -Quiet } # fast collection
@(Get-Service | Where Status -eq Stopped).Count # @() forces array even for 0/1 result
A command returning one object gives a scalar, several give an array — wrap in @() when you rely on .Count or indexing.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in PowerShell → Arrays. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/powershell/ · confidence 94%Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
PowerShell → Arrays reference map
Fast lookup boundaries for PowerShell → Arrays.
| 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 |