Today, Get-ChildItem had me tearing my hair out.  Have a look at the following; you can play along at home!

  • Make a folder  c:\Test
  • Make a text file in there called  File.txt
  • Make a subfolder in there called  SubFolder
  • Make a text file in that subfolder called  ChildFile.txt

Now try the following and predict what you will see in advance.

Get-ChildItem -Path 'c:\Test' -File
Get-ChildItem -Path 'c:\Test' -File -Recurse
Get-ChildItem -Path 'c:\Test' -File -Recurse -Exclude '*.pdf'
Get-ChildItem -Path 'c:\Test' -File -Exclude '*.pdf'

What I expected to see was commands 1 and 4 would do the same thing and 2 and 3 would do the same thing.  But NO, command 4 does not show the file sitting in the folder  c:\Test whereas command 3 does!

I’ve eventually found a clue in the help for the -Include parameter!


There’s no such note made against the -Exclude parameter but sure enough, if we do what this suggests, we get the desired result.

Get-ChildItem -Path 'c:\Test\*' -File -Exclude '*.pdf'

That’s really weird and I don’t understand the logic.  I want to get the child items of a folder whose path I’ve specified, why should I have to append a ‘*’ to the path too?  One of the examples for the command says a bit more on the subject but I still don’t follow their logic:


It says the wildcard indicates the contents of the directory rather than the directory container.  Is not requesting the “child items” of a directory container the same as requesting the directory contents?!  Continuing the logic they state, why then does  Get-ChildItem -Path ‘c:\Test’ -File  return any files at all?