27
dir/b > files.txt

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

Przemysław Banaszek
  • 838
  • 2
  • 11
  • 17

7 Answers7

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

Joey
  • 344,408
  • 85
  • 689
  • 683
18

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

Get-ChildItem -name (or dir -name)

Torbjörn Bergstedt
  • 3,359
  • 2
  • 21
  • 30
  • Nice. Didn't know that yet. I usually go the "Produce objects, project into something useful" route. – Joey Jan 12 '14 at 11:37
5

Simply put:

dir -Name > files.txt
Jet
  • 51
  • 1
  • 1
  • 2
    you should really add some reference or documentation for this or better explain it – GµårÐïåñ Nov 01 '17 at 02:26
  • Sorry, but the other posts seem to have not been provided with documentations either. Anyway, I can't get a proper documentation for my answer, but I use it as 'the equivalent of dir /b > files.txt' which works favorably. – Jet Nov 02 '17 at 17:14
  • So if everyone jumps off a bridge, that makes it ok for you to do it too? You should only care about the quality of your own work, not compare it to the others. We are providing technical solutions, which always by nature of the industry standard require some kind of explanation, elaboration and references in the least of the cases. Simple as that really, you don't want to do it, that's fine but don't expect your answer to receive quality attention or be received well in general. It was to help you, not a recrimination, take the constructive suggestion, don't fight or justify it. – GµårÐïåñ Nov 02 '17 at 19:33
  • I appreciate your comment and good intention, sir. However, it is not because I don't want to provide it, but it is by far the best I could provide. I've been searching for a proper documentation and, unfortunately, couldn't acquire one. Without one, the answers are merely hack at best, but hey, if I see everyone jumps off a bridge and it's the only way to get down there but somehow landed on their feet, then it's worth the shot. I would have only jumped a little differently, which in my opinion is more efficient. I'll provide a proper reference in the future if I get my hands into one. – Jet Nov 03 '17 at 03:25
  • I really hope that my comment didn't come across as snooty because that is the furthest from my intention to provide constructive feedback. But I will say, if you have a parachute or think you can land on your feet, by all means take the leap ;) Now all seriousness, it was meant to help you provide better information and instill sense of confidence in people following it, that's all and if you ever find it, come back and update your answer because people WILL come across it at some point, it would be great if they can learn from it. After all at some point you had to learn it yourself somehow. – GµårÐïåñ Nov 03 '17 at 20:32
  • `dir -?` couldn't find the help files on my computer but provided a link to https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-6 which explains the `-Name` parameter. – Jens Piegsa Aug 12 '19 at 18:46
4

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
Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
  • Hi Richard. Can you tell me why that code returns me an empty row between each item? – Nicola Cossu Apr 04 '11 at 10:51
  • 1
    @NickRulez: "Works on my machine". `Format-Table` will pad with spaces to specified width (default is current session's width): could that be it? Using `% {$_.Name}` (`%` = `foreach-object`) rather than format table will avoid this. – Richard Apr 04 '11 at 11:36
  • Thanks for your reply. :) If I use foreach-object everything works fine. The output of ft is right. The problem arises when I pipe to out-file. In this case I have an empty row, then file name, another empty row, filename and so on. I don't know how to check current session's width. – Nicola Cossu Apr 04 '11 at 12:01
  • Thanks for the "whole picture" answer. It comes out -encoding flag is not needed. – Przemysław Banaszek Apr 04 '11 at 12:07
  • Update. I was running the code from powershell ise editor. If I run it from command line I don't have empty rows except the first line of the file. – Nicola Cossu Apr 04 '11 at 12:17
  • powershell gives me 1 empty row, ise 2:) – Przemysław Banaszek Apr 04 '11 at 12:29
  • @Przemysław Banaszek. Do you have two empty rows only at the top of the file and no one between each file? – Nicola Cossu Apr 04 '11 at 12:32
  • @nickrulez: Try opening the output file in a text editor and using the end key to show where the end of line is (or [PSCX's](http://pscx.codeplex.com/) `Format-Hex` for a real old school approach). I think you'll see lots of spaces, causing line wrap and thus blank lines. `Format-Table` has a parameter to set the width. – Richard Apr 05 '11 at 07:16
  • @rzemysław: see last comment. – Richard Apr 05 '11 at 07:16
3

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 }
tenpn
  • 4,556
  • 5
  • 43
  • 63
2

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
1

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
BananaAcid
  • 3,221
  • 35
  • 38