ForEach statement will not loop into a $null array variable, while ForEach-Object cmdlet will loop it once with a $null value. This may bite you with NullExceptions.
Here is a code snippet to demonstrate the issue.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$myArray = $null | |
# No loop | |
ForEach($i in $myArray){ | |
'In ForEach' | |
$i | |
} | |
# Loop once with $null value | |
$myArray | ForEach-Object { | |
'In ForEach-Object' | |
$_ | |
} |