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.