Windows PowerShell equivalent of Linux `ls -l`

tmaynard picture tmaynard · Apr 27, 2015 · Viewed 28.3k times · Source

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.)

Answer

Florian Feldhaus picture Florian Feldhaus · Apr 27, 2015

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