Using the ls Command in Windows: Quick Start Guide

Updated

ls command in windows

ls works in PowerShell on Windows, but it is not the Unix ls. It is a built-in alias for Get-ChildItem, so it takes Get-ChildItem‘s parameters. ls -la does not list hidden files in long format — it fails with a parameter-binding error, because Get-ChildItem has no -la parameter. In Command Prompt there is no ls at all; the command is dir. If you want the real ls, with real Unix flags, run it inside WSL or Git Bash.

I have no Windows machine. Every Windows behaviour below is taken from Microsoft’s own reference pages, linked inline. Nothing here is presented as a session I ran on Windows.

What ls is in PowerShell 7.x

Microsoft’s Get-ChildItem reference for PowerShell 7.6 lists the aliases in its Notes section:

PowerShell includes the following aliases for Get-ChildItem:
- All platforms:
    - dir, gci
- Windows:
    - ls

Two things follow. The alias still exists in current PowerShell 7.x — it was not removed. And it is Windows-only: on the Linux and macOS builds of PowerShell, ls is deliberately left undefined so that the operating system’s real ls stays reachable. That split is the single fact that explains most of the confusion in this genre.

The flags that break

Because ls is just a name for Get-ChildItem, PowerShell’s parameter binder resolves whatever you type after it against Get-ChildItem‘s parameters and their documented aliases. Results, from that same reference page:

You type What PowerShell does
ls -l -LiteralPath is the only parameter starting with l; it requires a value, so the command errors
ls -a resolves toward -Attributes, which also requires a value
ls -la no parameter or alias matches la — parameter-binding error
ls -h h is a documented alias of -Hidden, so this shows only hidden items — not human-readable sizes
ls -r / ls -R / ls -s works: r and s are documented aliases of -Recurse

ls -h is the dangerous one: -h is a real parameter alias, so the command succeeds and quietly returns a different set of files instead of erroring. The correct spellings are Get-ChildItem -Force (include hidden and system items), Get-ChildItem -Recurse, and Get-ChildItem -Force -Recurse.

Command Prompt: use dir

cmd.exe has no ls, and Windows does not ship an ls.exe on the default PATH. Use dir, dir /a for hidden and system files, and dir /s to recurse.

Getting a real ls

Two supported routes, both giving you GNU coreutils ls where ls -la means what you expect:

  • WSL. From an elevated PowerShell or Command Prompt, run wsl --install, which installs the components and a default Linux distribution (Microsoft’s WSL install guide).
  • Git Bash, installed with Git for Windows. It bundles a MSYS2 userland, so ls, grep and friends behave as they do on Linux.

The alias tip that does not do what it looks like

A widely copied snippet is:

Set-Alias ll Get-ChildItem -Force

That does not create an ll that shows hidden files. Per Microsoft’s Set-Alias reference, -Force is Set-Alias’s own parameter: “Use the Force parameter to change or delete an alias that has the Option parameter set to ReadOnly.” It is never passed through to Get-ChildItem. The same page states the general rule: “You can’t create an alias for a command with parameters and values… To create an alias for a command, create a function that includes the command.”

So use a function, and put it in your profile ($PROFILE) — aliases and functions defined at the prompt vanish when the session closes:

function ll { Get-ChildItem -Force @args }

Piping to grep

PowerShell ships no grep. ls | grep "log" only works in Git Bash or WSL. In PowerShell, filter the objects instead of the text:

Get-ChildItem | Where-Object Name -like '*log*'
Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.