A collection groups objects so a script can test membership or process each value in turn. In PowerShell, arrays are a common collection form. section 5.1.2 section 5.3
Use collections for two common scripting tasks:
- Check whether an array contains a value with -contains, or place the value on the left and use -in. section 5.1.2
- Enumerate items with ForEach; the loop assigns one object at a time to a variable and exits after the collection is exhausted. section 5.3
- Prefer readable singular and plural variable names, such as $name and $names, while following command parameter conventions when necessary. section 5.3
$array = @("one", "two", "three")
$array –contains "one"
$array –contains "five"
"two" –in $array
"bob" –in $array
Run membership tests against a three-item array to see which values belong to the collection. section 5.1.2
$services = Get-Service –name bits,lanmanserver,spooler
Foreach ($service in $services) {
Restart-service $service –passthru
}
Process service objects one at a time with ForEach and restart each service with pass-through output. section 5.3















