13

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
  • Not an answer to your question directly, but if you wish to have ls styled directory output, there are ports of ls for Windows which do not require PowerShell, which you may want to consider. – Anaksunaman May 23 '18 at 10:18
  • I think I know what you want. You just want file names tab separated? – EBGreen May 23 '18 at 12:53
  • I cheat and use scoop. This will not give you items in the object level gci would, however it adds many of your favourite *nix tools to windows. – r007ed Dec 5 '19 at 15:02
7

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

2
  • 5
    Downvoted because the "why try to imitate Linux" part of the answer is unnecessary. OP's question was clearly about getting a more compact formatting of the output. – jmlorenzi Jun 25 '20 at 11:20
  • well, the OP accepted it as solving his use case, sooooo, not sure why you did this, but whatever. So, you are saying you know more about what the OP wanted than the OP expected/accepted. Wow, really! – postanote Jun 25 '20 at 20:06
10

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
3

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
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. Could you elaborate on your answer? – MMM Dec 5 '19 at 15:06
  • With aliases you can shorten this to ls -dir | fw Name -a or even ls -dir | fw -a – Dennis Jun 12 '20 at 14:00
2

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.

0

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
}
0

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.