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

倾然丶 夕夏残阳落幕 提交于 2019-12-03 10:46:59
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.

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

Get-ChildItem -name (or dir -name)

Richard

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

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 }

Simply put:

dir -Name > files.txt

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!