88

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" }
84

Try this:

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

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
-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
  • 11
    This (about directories) is not true. Nothing can stop you to have directory with extension – Romeo Ninov May 7 '15 at 11:51
  • 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. – Raúl Salinas-Monteagudo Feb 5 '20 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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