ranja
ranja

Reputation:

How do I do 'dir /s /b' in PowerShell?

I have a folder with three files and want the equivalent of the command prompt's dir /s /b command in PowerShell. How do I do that?

For example, if the folder name is temp3 and it contains three 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?

Upvotes: 90

Views: 154719

Answers (14)

jkwuc89
jkwuc89

Reputation: 1415

Following up on rony's answer above. I defined this alias inside $PROFILE to map dir to cmd /r dir with all arguments passed through.

Remove-Item alias:dir -Force
Function dir { cmd /r dir $args }

Upvotes: 0

Michael Reynolds
Michael Reynolds

Reputation: 1

I recommend using the tree /f command if you just wanted to see what dir /s/b did for you while in a powershell console:

C:\test>tree /f
C:.
├───a
├───anotherdir
│       somefile.txt
│       somefile2.txt
│       somefile3.txt
│
└───anotherdir2

If you are writing a script or something I like the answer @Bob gave:

(dir -r).FullName

Upvotes: 0

SebMa
SebMa

Reputation: 4727

To show files recursively (including hidden ones), I use :

$ ls -r -h | % fullname

Upvotes: 0

Ricky Lively
Ricky Lively

Reputation: 1

CMD /r DIR \\\\URL /s is VERY useful... PowerShell does not allow \\URL in Get-ChildItem alias ;)

Upvotes: -1

AntonioHL
AntonioHL

Reputation: 718

In PowerShell, the command-line to find files is Get-ChildItem that have aliases (gci,ls,dir). In the dir -?, you can find the url explanation : Get-ChildItem

Examples of commands:

dir *.txt -s | select name,length

ls *.txt -s | select fullname

Upvotes: 4

Andrew
Andrew

Reputation: 1

Get-ChildItem -Recurse | Select FullName

Upvotes: 0

Simon Li
Simon Li

Reputation: 1

if you are working interactively with PowerShell shell instead of writing a script, you can just shell-back to old cmd.exe by typing cmd and enter. then you can run dir /s /b as old times. Pay attention to the command line prompt to distinguish if you are in PS or CMD.

Upvotes: 0

cbuchart
cbuchart

Reputation: 11575

A variation of Bob answer is to use a pipe for realtime output (having a better feedback in large directories):

dir -r | % FullName

Upvotes: 11

Bob
Bob

Reputation: 131

This is equivalent:

(dir -r).FullName

Upvotes: 13

BananaAcid
BananaAcid

Reputation: 3501

Just to enforce, what Joey said:

gci -r -filter *.log | % fullname

This works to find files like dir /s/b *.log does.


(dir -r *.log).FullName works as well


Execute this once in your powershell shell, to enable a dirsb *.log command:

function global:dirsb {
    param ([Parameter(Mandatory=$true)][string]$fileFilter)
    gci -r -filter $fileFilter | % fullname
}

or add it to your profile: PS> notepad $profile

Upvotes: 8

rony l
rony l

Reputation: 6022

If you just want to permanently replace Powershell's dir alias (Get-ChildItem) with a call to cmd dir, for all future powershell windows you're going to open just do the following:

  1. notepad $profile (from powershell window)
  2. when file opens, insert the following rows and save:

    Remove-Item alias:\dir
    function dir($1, $2, $3, $4) {cmd /r dir $1 $2 $3 $4}
    

Upvotes: 5

fernacolo
fernacolo

Reputation: 7439

If you are using Powershell as a shell (and not as a script processor), you can simply type:

cmd /r dir /s /b

The /r flag tells cmd.exe to run the command and exit. In other words, you'll end at the same execution context.

For many commands, cmd /r is better than dealing with Powershell object-oriented architecture.

Upvotes: 87

Joey
Joey

Reputation: 354744

You can use

Get-ChildItem -Recurse | Select-Object -ExpandProperty FullName
gci -r | select -exp FullName

or

Get-ChildItem -Recurse | ForEach-Object { $_.FullName }
gci -r | % { $_.FullName }
gci -r | % FullName    # In recent PowerShell versions

(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.

Upvotes: 50

Ryan Bemrose
Ryan Bemrose

Reputation: 9266

Adding onto Joey's answer. Starting in PowerShell 3.0, you can use the new Foreach-Object shorthand to get the FullName property.

Get-ChildItem -Recurse | Foreach-Object FullName
gci -r |% FullName

The difference is that you don't need to use curly braces ({}) or the $_ variable if all you need is a property.

Upvotes: 8

Related Questions