Powershell enters foreach loop with null object
- by SteB
I'm listing all backups in a given directory:
$backups = Get-ChildItem -Path $zipFilepath |
Where-Object {($_.lastwritetime -lt (Get-Date).addDays(-7)) -and
(-not $_.PSIsContainer) -and ($_.Name -like "backup*")}
If I set it to deliberately return no files (.addDays(-600) then the following prints "Empty" (as expected):
if (!$backups)
{
"Empty"
}
If I try to list the names with an empty $backups variable:
foreach ($file in $backups)
{
$file.FullName;
}
I get nothing (as expected), if I change this to:
"test"+ $file.FullName;
then I get a single "test" under the "Empty". How is this possible if $backups is empty?