List the Files in a Directory Using Windows PowerShell

Uses Windows PowerShell to list the files in a folder.

4.9 Star
Add to favorites
E-mail Twitter Digg Facebook
  • How can I list the files in the subdirectories?
    1 Posts | Last post September 24, 2013
    • Can you easy change the script so it will also look at the subdirectories?
  • you serious?
    3 Posts | Last post August 22, 2013
    • > ls # lists everything in a directory
      > ls | ?{ $_.Mode[ 0 ] -ne "d" } # lists only files in a directory
      theres probably a more correct way than number 2, but i'm a ps noob. 
    • Would this work the same as well:
      get-childitem -r | outfile c:\scripts\test.txt
    • I agree with bossplayers. GCI or get-childitem is designed for this exact requirement. 
      The proposed solution ignores most of the power of powershell & puts you in an uncomfortable position. Writing powershell like it was VBScript/WMI or C.
      1. Given most Powershell Commands take objects as input. It is cleaner to pass the results thru a pipeline rather than creating your own loop. 
      Example: The following code does the same thing.
         Get-ChildItem("E:\!Code") | Format-Table name
      If you leave out "name", it displays all file attributes. Add -AutoSize, and the columns fit on the screen better. Change Format-Table to Format-List for even more details. (ie VersionInfo)
      2. As Powershell has evolved there is less need to rely in WMI constructs like [IO.Directory] & [Console]. 
  • what if you are accessing diferent server folders like getfiles(\\prodserver\c$\path)
    1 Posts | Last post January 22, 2013
    • [IO.Directory]::GetFiles(\\server\c$\path) 
      use different credentials