1

I did a ton of reading and searching about a way to have Get-ChildItem return a dir listing in wide format, in alphabetical order, with the number of files and directories in the current directory. Here is a image of what I ended up with, but not using GCI.

I ended up writing a small PS file.

$bArgs = "--%/c"
$cArgs = "Dir /n/w"
& cmd.exe -ArgumentList $bArgs $cArgs

As you can see I ended up using the old cmd.exe and passing the variables I wanted. I made an alias in my PS $Profile to call this script. Can this not be accomplished in PS v5.1? Thanks for any help or advice for an old noob.

Toto
  • 89,455
  • 62
  • 89
  • 125
b4iknew
  • 23
  • 6

1 Answers1

0

PowerShell's for-display formatting differs from cmd.exe's, so if you want the formatting of the latter's internal dir command, you'll indeed have to call it via cmd /c, via a function you can place in your $PROFILE file (note that aliases in PowerShell are merely alternative names and can therefore not include baked-in arguments):

function lss { cmd /c dir /n /w /c $args }

Note that you lose a key benefit of PowerShell: the ability to process rich objects:

  • PowerShell-native commands output rich objects that enable robust programmatic processing; e.g., Get-ChildItem outputs System.IO.FileInfo and System.IO.DirectoryInfo instances; the aspect of for-display formatting is decoupled from the data output, and for-display formatting only kicks in when printing to the display (host), or when explicitly requested.

    • For instance, (Get-ChildItem -File).Name returns an array of all file names in the current directory.
  • By contrast, PowerShell can only use text to communicate with external programs, which makes processing cumbersome and brittle, if information must be extracted via text parsing.


As Pierre-Alain Vigeant notes, the following PowerShell command gives you at least similar output formatting as your dir command, though it lacks the combined-size and bytes-free summary at the bottom:

Get-ChildItem | Format-Wide -AutoSize

To wrap that up in a function, use:

function lss { Get-ChildItem @args | Format-Wide -Autosize }

Note, however, that - due to use of a Format-* cmdlet, all of which output objects that are formatting instructions rather than data - this function's output is also not suited to further programmatic processing.

A proper solution would require you to author custom formatting data and associate them with the System.IO.FileInfo and System.IO.DirectoryInfo types, which is nontrivial however.

See the conceptual about_Format.ps1xml help topic, Export-FormatData, Update-FormatData, and this answer for a simple example.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Just wondering why my reply to [mklement0](https://stackoverflow.com/users/45375/mklement0) was deleted? Any way, thanks again [mklement0](https://stackoverflow.com/users/45375/mklement0) – b4iknew Jul 20 '21 at 10:10
  • 2
    @b4iknew I don't know why, but [“thank you” comments are generally discouraged](https://meta.stackexchange.com/q/145551/230282) so many high-rep users and mods will delete it. One should upvote and accept instead – phuclv Jul 20 '21 at 12:39
  • 1
    phuclv is correct, @b4iknew: I flagged your comment as no longer needed, and a moderator removed it as a result. To be clear, I genuinely appreciated your nice feedback; since I perceived your comment as a personal thank-you, I thought it had served its purpose once I had read it. To future readers it is only comments with technical information that are of interest. Similarly, if I should thank _you_ in a comment in the future or again respond to a thank-you from you, I encourage you to flag that comment as no longer needed as well after reading it - including this one. – mklement0 Jul 20 '21 at 12:50
  • 1
    @b4iknew, please also see my update to the answer. – mklement0 Jul 20 '21 at 12:50