Why Is It Possible to Loop Through a Null Array
powershell
Solution
This was an intentional design choice made in V1 and revisited in V3.
In most languages, the foreach statement can only loop over collections of things. PowerShell has always been a little different, and in V1, you could loop over a single value in addition to collections of values.
For example:
foreach ($i in 42) { $i } # prints 42
In V1, if a value was a collection, foreach would iterate over each element in the collection, otherwise it would enter the loop for just that value.
Note in the above sentence, $null isn't special. It's just another value. From a language design point of view, this is fairly clean and concisely explained.
Unfortunately many people did not expect this behavior and it caused many bugs. I think some confusion arises because people expect the foreach statement to behave almost like the foreach-object cmdlet. In other words, I think people expect the following to work the same:
$null | foreach { $_ }
foreach ($i in $null) { $i }
In V3, we decided that it was important enough to change behavior because we could help scripters avoid introducing bugs in their scripts.
Note that changing the behavior could in theory break existing scripts in unexpected ways. We ultimately decided that most scripts that potentially see $null in the foreach statement already guard the foreach statement with an if, e.g.:
if ($null -ne $c)
{
foreach ($i in $c) { ... }
}
So in reality, most real world scripts would not see a change in behavior.
Problem
Given the following PowerShell code: ``` $FolderItems = Get-ChildItem -Path "C:\Test" Write-Host "FolderItems Is Null: $($FolderItems -eq $null)" foreach ($FolderItem in $FolderItems) { Write-Host "Inside the loop: $($FolderItem.Name)" } Write-Host "Done." ``` When I test it with one file in the `C:\Test` folder, it outputs this: ``` FolderItems Is Null: False Inside the loop: MyFile.txt Done. ``` However, when I test it with ZERO files in the folder, it outputs this: ``` FolderItems Is Null: True Inside the loop: Done." ``` If `$FolderItems` is null, then why does it enter the `foreach` loop?