Przemysław Banaszek
Przemysław Banaszek

Reputation: 838

equivalent of (dir/b > files.txt) in PowerShell

dir/b > files.txt

I guess it has to be done in PowerShell to preserve unicode signs.

Upvotes: 27

Views: 59606

Answers (7)

BananaAcid
BananaAcid

Reputation: 3501

I am using:

(dir -r).FullName > somefile.txt

and with filter for *.log:

(dir -r *.log).FullName > somefile.txt

Note:

dir         is equal to `gci` but fits the naming used in cmd
-r          recursive (all subfolders too)
.FullName   is the path only

Upvotes: 1

Stefan B. Christensen
Stefan B. Christensen

Reputation: 31

Just found this great post, but needed it for sub directories as well:

DIR /B /S >somefile.txt

use:

Get-ChildItem -Recurse | Select-Object -ExpandProperty Fullname | Out-File Somefile.txt

or the short version:

ls | % fullname > somefile.txt

Upvotes: 2

Jet
Jet

Reputation: 51

Simply put:

dir -Name > files.txt

Upvotes: 5

Joey
Joey

Reputation: 354744

Get-ChildItem | Select-Object -ExpandProperty Name > files.txt

or shorter:

ls | % Name > files.txt

However, you can easily do the same in cmd:

cmd /u /c "dir /b > files.txt"

The /u switch tells cmd to write things redirected into files as Unicode.

Upvotes: 48

Torbjörn Bergstedt
Torbjörn Bergstedt

Reputation: 3429

Get-ChildItem actually already has a flag for the equivalent of dir /b:

Get-ChildItem -name (or dir -name)

Upvotes: 18

Richard
Richard

Reputation: 109130

In PSH dir (which aliases Get-ChildItem) gives you objects (as noted in another answer), so you need to select what properties you want. Either with Select-Object (alias select) to create custom objects with a subset of the original object's properties (or additional properties can be added).

However in this can doing it at the format stage is probably simplest

dir | ft Name -HideTableHeaders | Out-File files.txt

(ft is format-table.)

If you want a different character encoding in files.txt (out-file will use UTF-16 by default) use the -encoding flag, you can also append:

dir | ft Name -HideTableHeaders | Out-File -append -encoding UTF8 files.txt

Upvotes: 4

tenpn
tenpn

Reputation: 4716

Since powershell deals with objects, you need to specify how you want to process each object in the pipe.

This command will get print only the name of each object:

dir | ForEach-Object { $_.name }

Upvotes: 3

Related Questions