102

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" }
Frank Schwieterman
  • 1,375
  • 3
  • 11
  • 19

4 Answers4

155

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
Chris Magnuson
  • 297
  • 1
  • 7
  • 11
iraSenthil
  • 1,663
  • 1
  • 10
  • 6
99

Try this:

gci . *.* -rec | where { ! $_.PSIsContainer }
Andy
  • 1,387
  • 12
  • 12
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 }
Dominique
  • 1,993
  • 9
  • 30
  • 65
-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.