Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows PowerShell equivalent of Linux `ls -l`

Tags:

powershell

ls

Is there some way in PowerShell to emulate the behavior of the Linux ls -l command?

I would like to see what symbolic links point to. Specifically, I used the PowerShell New-Symlink utility and I want to know where the -Path is on disk.

(Yes, I know the -Path because I created it myself, but I've wanted an ls -l equivalent on several other occasions.)

like image 267
tmaynard Avatar asked Apr 27 '15 04:04

tmaynard


People also ask

What is the equivalent of ls in PowerShell?

Windows “dir” Command is “ls” Command Equivalent Windows MS-DOS and PowerShell command-line interface provide the dir command in order to list files and folders. By default without any option, the dir command lists files and folders.

What is ls in Linux in Windows?

The “ls” command (that's LS, not IS) is one of the first terminal commands that veterans teach Linux beginners. It allows users to list files and directories from the Command Line Interface. You can think of it as a File Explorer, but without the user-friendly icons and navigation buttons.

What is PowerShell equivalent on Linux?

Other interesting Linux alternatives to PowerShell are Tabby Terminal, Hyper, GNOME Terminal and fish. PowerShell alternatives are mainly Terminal Emulators but may also be Shells or SSH Clients. Filter by these if you want a narrower list of alternatives or looking for a specific functionality of PowerShell.


2 Answers

With PowerShell v. 6 and above it is now built-in that Get-ChildItem will show symbolic links with their target location.

Get-ChildItem Docs

For emulating Linux ls -l you can do something like this:

Get-ChildItem | Format-Table Mode, @{N='Owner';E={(Get-Acl $_.FullName).Owner}}, Length, LastWriteTime, @{N='Name';E={if($_.Target) {$_.Name+' -> '+$_.Target} else {$_.Name}}}

This matches the column order of ls -l and also adds the file owner which Get-ChildItem doesn't show. If the file is a symlink, it will show in Mode column with l and the file name with show like so:

filename.txt -> c:\Users\joe\filename.txt

Here my functional version which I aliased to ll and added to my profile:

function Get-ChildItemUnix {
    Get-ChildItem $Args[0] |
        Format-Table Mode, @{N='Owner';E={(Get-Acl $_.FullName).Owner}}, Length, LastWriteTime, @{N='Name';E={if($_.Target) {$_.Name+' -> '+$_.Target} else {$_.Name}}}
}
New-Alias ll Get-ChildItemUnix

Output shows like so: enter image description here

like image 93
David Hatch Avatar answered Sep 21 '22 06:09

David Hatch


It seems powershell does not support this out of the box. Here's an article explaining how to use dir in PowerShell to show if there is a symbolic link, but it doesn't show the target of the link. To show the target of a link you have two options. First use fsutil reparsepoint query or use dir from the command prompt. Both have drawbacks. The first gives you the target path as hex data and the second must be run from the command prompt as powershell aliases dir to Get-ChildItem which doesn't show symlinks.

Try this

Get-ChildItem | % { fsutil reparsepoint query $_ }
cmd /c dir /a:l

The best answer I could come up with is the following (use the regex matching groups to extract more information if required):

$PATH="C:\tmp"
cmd /c dir $PATH | ? { $_ -match '([\d\.]+)\s+([\d:]+)\s+(\S+)\s+([^\[]+)\s*\[?([^\]]*)\]?' } | % { New-Object -TypeName PSCustomObject -Property @{Name=$matches[4];Target=$matches[5]}}

The Output will look similar to this

Target                                                      Name
------                                                      ----
                                                            .
                                                            ..
                                                            file.txt
C:\tmp\file.txt                                             symlink.txt
like image 29
Florian Feldhaus Avatar answered Sep 22 '22 06:09

Florian Feldhaus