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.

3

6 Answers

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.

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

2

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.

2

I wrote a package a while back to do just this! You can read the installation instructions here:

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.

1

If you have git installed (and we all do, right?), you can use the ls that's bundled with git. Benefits are that it's a fast native binary and works with Windows path separators. Here's what it looks like:

ls output like on Linux

Put the following code in your PowerShell profile (its filepath is inside the $profile variable):

if ($host.Name -eq 'ConsoleHost')
{ function ls_git { & 'C:\Program Files\Git\usr\bin\ls' --color=auto -hF $args } Set-Alias -Name ls -Value ls_git -Option Private
}

create a $profile variable with

New-Item $profile -Type File -Force

It creates a file called Microsoft.PowerShell_profile.ps1 in a folder called WindowsPowerShell under your Documents folder. Then you can open it with a text editor:

notepad $profile

add this to your $profile variable.

if ($host.Name -eq 'ConsoleHost')
{ function ls_git { & 'C:\Program Files\Git\usr\bin\ls' --color=auto -hF $args } Set-Alias -Name ls -Value ls_git -Option AllScope
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like