Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

People also ask

What is the equivalent of dir B in PowerShell?

The direct equivalent of that command in PowerShell would be dir -n > dirlist.

What is the PowerShell command for dir?

The dir cmdlet in Windows PowerShell generates a list of File and Folder objects, which PowerShell formats into a text listing after the fact.

How do I list a directory in PowerShell?

On a Windows computer from PowerShell or cmd.exe, you can display a graphical view of a directory structure with the tree.com command. To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property. You can use the Recurse parameter with Directory.

How do I get a list of files in a directory in PowerShell?

If you want to list files and directories of a specific directory, utilize the “-Path” parameter in the “Get-ChildItem” command. This option will help PowerShell list all the child items of the specified directory. The “-Path” parameter is also utilized to set the paths of one or more locations of files.


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.


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.


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.


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


This is equivalent:

(dir -r).FullName

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}