tags:

views:

205

answers:

1

I have a folder with 3 files and want the equivalent of dir /s /b in powershell. How do I do that?

e.g. if the folder name is temp3 and it contains 3 text files - a.txt. b.txt, and c.txt, doing

C:\temp3>dir /s /b

gives me

C:\temp3\a.txt
C:\temp3\b.txt
C:\temp3\c.txt

How do I get the same result in powershell?

+6  A: 

You can use

Get-ChildItem -Recurse | Format-Table FullName -HideTableHeaders
gci -r | select -exp FullName

or

Get-ChildItem -Recurse | ForEach-Object { $_.FullName }
gci -r | % { $_.FullName }

(The long version is the first one and the one shortened using aliases and short parameter names is the second, if it's not obvious. In scripts I'd suggest using always the long version since it's much less likely to clash somewhere.)

Re-reading your question, if all you want to accomplish with dir /s /b is to output the full paths of the files in the current directory, then you can drop the -Recurse parameter here.

My advice to you, though: Don't use strings when you can help it. If you want to pass around files, then just take the FileInfo object you get from Get-ChildItem. The cmdlets know what to do with it. Using strings for things where objects work better just gets you into weird problems.

Joey
Awesome! Thanks dude.
You can also do:gci -r | select -expand fullname
x0n
@Johannes: it does work. You using v2 final? See http://blogs.msdn.com/powershell/archive/2009/09/14/select-expandproperty-propertyname.aspx
Richard Berg
Hm, no, to my shame I looked it up in v1 (I was at the only machine near me that still has v1). And I even read that blog post but didn't remember it.
Joey