I want to create a PowerShell script that returns a bunch of file in a given list of directories. This is the function:
function CheckFilesToDelete([string[]]$fList) {
[System.Collections.Generic.List[System.IO.DirectoryInfo]]$fileList
foreach($folderName in $fList)
{
$path = "$folderName"
($list = @(Get-ChildItem "$path" -File -Recurse |
Where-Object { $_.Name -match '.*TMP.*' })) > null
if($list -ne $null -and $list.Count -gt 0) {
$fileList.AddRange($list)
}
}
return $fileList
}
The problem I have is where I try to check if the $list
has a value and add the $list
to the $fileList
. The error is
RuntimeException: You cannot call a method on a null-valued expression
I don't know how to fix it.