Powershell – How to Produce a Linux-like `ls` Output in Powershell

powershellwindows

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.

Best Answer

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

Related Question