使用 PowerShell 的 Get-ChildItem 列出目录内容时如何省略目录名称和空行?

使用 PowerShell 的 Get-ChildItem 列出目录内容时如何省略目录名称和空行?
PS C:\Users\user> Get-ChildItem | Format-Wide -AutoSize


    Directory: C:\Users\user



Contacts                                                 Cookies
Desktop                                                  Documents
Downloads                                                Favorites


PS C:\Users\user>

是否有一种方便的方法可以省略目录头以及输出中的 7 个空行?

如果解决方案与以下方面配合良好,则可获得加分目录颜色

答案1

对于一个非常简单的例子,我会选择名称:

PS> (Get-ChildItem).Name
.docker
.ssh
.vscode
3D Objects
Calibre Library
Code
Contacts
Documents
Downloads
EneticsNC
Favorites

答案2

这允许使用Format-WIde并删除前导和尾随的空白行:

(Get-ChildItem | Select name | Format-Wide | Out-String).Trim("$([char]0x0a+[char]0x0d)")

输出:

PS C:\...\keith>(gci | select name | format-wide | out-string).Trim("$([char]0x0a+[char]0x0d)")
Contacts                                     Desktop
Documents                                    Downloads
Favorites                                    Links
Music                                        OneDrive
Original 3D Content                          Pictures
Sandbox                                      Saved Games
Screenshots                                  Searches
Standalone Programs                          Videos
2002271222HotKeys.reg                        2002271230HotKeys.reg
5f7b5f1e01b83767.automaticDestinations-ms    A20200807.txt
adstest.txt                                  AllFolders-StandardDetails.reg
arrByte.xml                                  atesty.reg
audioprops.txt                               B20200807.txt
BadTableWithDups                             bytes2.txt
Documents2002271232HotKeys.reg               DTShortcut.ps1
dummy.ini                                    env.txt
File Explorer.lnk                            filelist.txt
file_ex1_ex2.ex3                             gci.txt
help.txt                                     hex.bin
hkcrlevel1.txt                               hkcrlevel2.txt
HKLMAllFoldersDetails.txt                    hotkeys.xml
junctions.txt                                list.txt
listtxt                                      mkdir.txt
names.txt                                    NoEmpties.xml
out.txt                                      out2.txt
out3.txt                                     outnum.txt
PicLibTVBytes.txt                            Profcopy.ps1
redirect.txt                                 RegCommandExamples.txt
reginihelp.txt                               Relative.lnk
sc.lnk                                       script.ps1
set-clipboard                                Settings.reg
sha256.txt                                   sha2562.txt
star.txt                                     test (2).txt
test.txt                                     transTest.txt
usf.txt                                      WithEmpties.xml
PS C:\...\keith>

答案3

-Name您可以尝试在使用时将参数与命令一起使用Get-ChildItem。其输出不显示目录名称。

Get-ChildItem -name | Format-Wide -AutoSize -Force

笔记:我也使用了-Force的参数Format-Wide

图片:

图片:

相关内容