How to produce a Linux-like `ls` output in Powershell?

I don't want the output of ls(Get-ChildItem) to be vertical (one file one line) in Powershell. I want it to be horizontal (like a table in Linux). After searching the web I don't know how to do that.

And ls -n | Format-Table doesn't work. It's still vertical.

I'm not a native English speaker, so forgive me for some grammar mistakes.


I am not really sure why you'd want to do this on Windows or expect a Linux file system listing on Windows. Yet, but this is about a close as you will get, with Windows PowerShell natively.

#Collect the path listing, split on the line feed, join with a space delimiter 
(ls -n) -split "`n" -join " "

The Windows file system just does not natively list files this way by design, and Windows PowerShell's goal is not to mimic what other OS's file systems do. Windows file system will use its native a single string list, by design and not the table-like view in *NIX. No color highlighting of file or directories either.

Before you ask, no, you cannot just use Format-Table to what I am showing. If you want this look, then you need to write your own wrapper for LS/GCI or Use PoSHv6 on *NIX or OSX, or use a 'ls' port as noted by the other response or if you are on Win10, enable WSL (Bash on Linux) and just use WSL instead of Win PoSH.

You can of course only select file or directory list as well.

(ls -n -directory) -split "`n" -join " "

(ls -n -file) -split "`n" -join " "

You can use the Format-Wide cmdlet, depending on what PoSH version you are on.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-wide?view=powershell-6

ls | Format-Wide -Column 5

But what you cannot do is...

ls -n | Format-Wide -Column 5

...it will fail, no workaround that way.

You have to do stuff like this... work.

ls | Format-Wide -Column 5 -Property Name

... since it is the Format-Wide cmdlet doing this


If you have Windows Subsystem for Linux installed (WSL) you can call ls directly.

Typing

wsl ls 

Actually calls the linux ls command and gives you the same output you'd find on linux.

You can update the "ls" alias so it always call this using Powershells "Set-Alias". In the Powershell profile file (Microsoft.PowerShell_profile.ps1) do:

function ls_alias { wsl ls --color=auto -hF $args }
Set-Alias -Name ls -Value ls_alias -Option AllScope

That also allows you to pass in additional parameters, for instance show all files with:

ls -a

Note though: You'll have to pass in a Unix style path, not a windows one or it will not be recognised. For instance, this:

ls /mnt/c/tmp/my_file

but not:

ls C:\tmp\my_file

Try this command:

Get-ChildItem $args -Exclude .*  | Format-Wide Name -AutoSize

and you can always add an alias in the Powershell profile file (Microsoft.PowerShell_profile.ps1):

function l { Get-ChildItem $args -Exclude .*  | Format-Wide Name -AutoSize }

Or if you want to see the hidden files too:

Get-ChildItem $args -Force | Format-Wide Name -AutoSize

If you are wondering what is happening here, these are base Powershell commands. Get-ChildItem is the base command for dir and ls. Format-Wide takes a list and formats it.


I wrote a package a while back to do just this! You can read the installation instructions here: https://github.com/JRJurman/PowerLS

As far as how it works under the hood, there is some parsing of the the files and folders in the directory, and then it uses write-host with some color flags to generate a pretty nice output. Check the readme in the repo for examples.