Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Arrays

Direct 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.

When to use this

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.

First 30 seconds

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) {}.

Operational verification

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

Evidence · next step

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
Common mistakes
Common traps

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.

Platform / version applicability
Windows execution, automation and observability
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformWindows execution, automation and observability
Scope / prerequisiteUse this topic when a command sometimes returns a scalar and sometimes a collection, indexing behaves unexpectedly, pip…
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

1 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points
  • 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.
Key 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
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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docWindows execution, automation and observability current

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%
Terminology layer

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.

Aliases that resolve here
powershell arrayspowershell collection scalar arraypowershell array countpowershell unary comma
Embedded reference data

Quick lookup tables

PowerShell → Arrays reference map

Fast lookup boundaries for PowerShell → Arrays.

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
Confirmation

Confirm action

Continue?