105

I'd like to use get-childitem recursively, but only have it return files not directories. The best solution I have just doesn't seem natural:

gci . *.* -rec | where { $_.GetType().Name -eq "FileInfo" }

4 Answers 4

101

Try this:

gci . *.* -rec | where { ! $_.PSIsContainer }
1
  • 9
    if you're excluding containers then you can leave off the * . *, right?
    – MDMoore313
    Sep 1, 2016 at 13:33
159

In Powershell 3.0, it is simpler,

gci -Directory #List only directories
gci -File #List only files

This is even shorter,

gci -ad # alias for -Directory
gci -af # alias for -File
1
  • 2
    gci -File -Name to get only the names of the files Nov 16, 2021 at 18:26
0

This is not working when anyone named the folder xxx.PDF:

get-childitem -Recurse -include *.pdf

Best then you can do:

Get-ChildItem -Recurse -Include *.pdf | where { ! $_.PSIsContainer }
2
  • 1
    Some explanations are welcome.
    – Toto
    Feb 13, 2023 at 10:17
  • Are you running powershell on Unix or another case-sensitive platform? I wonder if *.pdf needs to be *.PDF to match those files. Feb 22, 2023 at 2:01
-5

In powershell 2.0 the best and simplest solution i came up with is to include all files with an extension:

get-childitem -Recurse -include *.*

folders doesn't have an extension so they are excluded, beware of no extension named files.

2
  • 14
    This (about directories) is not true. Nothing can stop you to have directory with extension May 7, 2015 at 11:51
  • 1
    Bold assertion of yours, "folders don't have an extension" It's quite typical to have dots in folder names, for example when they represent versions. Feb 5, 2020 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .