safesphere
safesphere

Reputation: 146

How to exclude folders from a recursive list of path\name with Get-ChildItem in Powershell 2.0 (similar to dir /s /b /a-d in DOS)?

I need to list recursively all files with paths, but without the lines for folders, something like this:

dir-name1\file-name1.ext
dir-name1\file-name2.ext
dir-name2\file-name3.ext
dir-name2\file-name4.ext

I use Powershell 2.0 that doesn't recognize some modern syntax. I've tried many suggested solutions, but none works. If it excludes folders, it also removes the file path; if it keeps the path, it doesn't exclude folders.

The only workaround I have so far with the obvious limitations is:

Get-ChildItem -Recurse -Name -Include *.*

Is there a better way without the complexity of writing a script?

Upvotes: 0

Views: 62

Answers (1)

Theo
Theo

Reputation: 61198

As promised, here my comment as short answer. In PowerShell version 2.0, the Get-ChildItem cmdlet doesn't have switches for -File or -Directory, so in order to get a listing of just files and no directories, you need to filter the DirectoryInfo objects out using a Where-Object clause:

Get-ChildItem -Path D:\Test -Recurse |      # enumerate ALL objects in the path
    Where-Object { !$_.PsIsContainer } |    # filter to receive only FileInfo objects
    Select-Object -ExpandProperty FullName  # return only the full path and file names

If you upgrade your now ancient version of PowerShell to at least version 5.1, you can get the same result (just faster) like this:

(Get-ChildItem -Path D:\Test -Recurse -File).FullName

Upvotes: 1

Related Questions