SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What the crap is this about?

I whipped up a fairly simple run-once and discard PowerShell script to iterate through all of the files in a list and make a simple change. The problem is every SPFile I get back is null. Every last one of them.

Now I understand that sometimes this is the case (like with forms: SPFile.Item == null and SPFolder.Item == null), but not on every last frigging file. I ran null checks up the hierarchy of my objects, and $folder is not null, but $file is. Seriously, what is wrong with this?

$web = Get-SPWeb $webUrl
$list = $web.GetList($listUrl)
foreach ($folder in $list.Folders)
    foreach ($file in $folder.Files)
        // $file is null here
share|improve this question
up vote 3 down vote accepted

Oh man, SPList.Folders actually returns an SPListItemCollection, not an SPFolderCollection...

However, SPList.RootFolder.SubFolders does return an SPFolderCollection...

share|improve this answer
I was under the same impression once that SPList.Folders returns SPFOlderCollection.… – Nadeem Yousuf Aug 15 '13 at 3:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.