ForEach
ForEach-Object:
- Collecting values from a member(property or method) by
-MemberName - Value transformation by
-Process - Mimicking a function that accepts pipeline input by
-Begin,-Process,-Endand-RemainingScripts - Parallel Jobs
Intrinsic ForEach has some overlap with ForEach-Object, it's not recommended but worth noting.
- Type conversion
- Collecting values from a member
- Replace a property by new value
TIP
Use foreach or % alias for ForEach-Object.
Collecting Values
- What differs
ForEach-ObjectfromSelect-Object -ExpandPropertyis-MemberNamecan accept a method name as well.
NOTE
See: help % -Parameter MemberName
ps1
# -MemberName is positional
gci | foreach -MemberName Name
# equivalent to
gci | select -ExpandProperty Name1
2
3
4
2
3
4
Evaluating from a method member is available, you can even pass parameters by -ArgumentList.
ps1
(1..3 | % GetType | select -first 1) -is [System.Type] # True
'1,2,3' | foreach -MemberName Split -ArgumentList ','
# or this, ',' is passed as ValueFromRemainingArguments for -ArgumentList
'1,2,3' | foreach Split ','1
2
3
4
5
2
3
4
5
Value Transformation
The way -Process behaves is collecting implicitly returned values as an array. Every implicitly returned value will be collected as a item.
NOTE
See: help % -Parameter MemberName
ps1
# -Process is positional at 0
gci | foreach -Process { $_.Exists, $false } # True, False, True, False...1
2
2
If you do want a $null return, use Out-Null to swallow the value.
ps1
# doesn't make much sense though
$null -eq (gci | foreach { $_.Name | Out-Null }) # True1
2
2
Intrinsic ForEach
Type Conversion
One of overloads of ForEach takes a System.Type, and trying to cast all of them to the type.
ps1
(65..90).ForEach([char]) # A-Z1
Collecting Values
ps1
('1,2,3').ForEach('Split', ',') # ArgumentList allowed
(gci -file).ForEach('Length')1
2
2
Override Property Value
ps1
(gci -file).ForEach('CreationTime', (Get-Date))1