In PowerShell, the most powerful way to list files is to use the `ls` command alias for Get-ChildItem, which retrieves all files and folders in the current directory with full details—file sizes, dates, and permissions—all in one line. You don’t need any prerequisites beyond the default PowerShell installation, and it works across Windows versions from 7 to 11. Unlike older cmdlets like dir, which only show basic info, Get-ChildItem gives you precise metadata and extensibility for scripting.

What You'll Need

  • Windows 10 or 11 (PowerShell 5.1+), or Windows Server 2019+—all include the Get-ChildItem cmdlet by default.
  • An open PowerShell window—either the default one (search "PowerShell" in the Start menu) or the new PowerShell 7+ on Ubuntu/Linux.
  • Basic familiarity with Windows file paths (e.g., C:\Users\YourName\Documents), though the command handles them automatically.

Step-by-Step

  1. Open PowerShell - Windows 10/11: Press Win+X, select Windows Terminal (Admin) or the classic PowerShell from the Start menu. If prompted by UAC, click Yes to run as administrator for full permissions (though most listings don’t require it). - Windows Server: Use the built-in PowerShell prompt in the Server Manager or run it from an elevated command line.
  2. Check basic directory contents Run this command in the current directory to see all files and folders: ls This shows: - File names with icons (if PowerShell Desktop is installed). - File sizes (e.g., 12K for 12KB). - Last modified date. - Permission flags (e.g., R for read, W for write). Example output: Directory: C:\Users\Public\Desktop Mode LastWriteTime Length Name ---- ------------- ----- ---- -rw- 5/15/2023 12:45PM 12345 Report.pdf drw- 7/10/2024 9:30AM 0 MyDocuments If you see Access Denied, try running PowerShell as administrator or use: ls -Force to bypass read-only checks.
  3. List files by extension Filter results with wildcards: ls *.txt lists all .txt files. ls *.log | Sort-Object LastWriteTime -Descending sorts logs by recency. For hidden files (e.g., `.config`), add -Force: ls -Force *.hidden.
  4. Recursively list subdirectories To explore folders deeply, use: ls -Recurse or specify a path: ls -Path "C:\Project\Subfolder" -Recurse. For large directories, add -Depth 1 to limit depth: ls -Depth 1 -Path "C:\" | Where-Object { $_.Name -ne "System" } (avoids the hidden Windows system folder).
  5. Export results to a file Save the output to CSV or a text file: ls | Export-Csv -Path "C:\output\files.csv" -NoTypeInformation For a plain text list: ls | Out-File -FilePath "C:\output\file_list.txt"
  6. Use with aliases and parameters Common shortcuts and flags:
    • ls -FullName shows full paths (e.g., C:\Temp\file.txt).
    • ls -ErrorAction SilentlyContinue skips errors (e.g., inaccessible files).
    • ls -TimeStamp forces date updates (useful for scripts).
    • ls * | Select-Object Name, Size, LastWriteTime customizes output columns.
    Example with aliases: gci -Recurse -Force | Where-Object { $_.Name -like "*test*" } (this uses gci alias for Get-ChildItem).

Common Mistakes

  • Running without admin rights - Missing files or folders may appear inaccessible. Fix: Run PowerShell as Administrator (right-click > Properties > Yes). For scripts, use Start-Process PowerShell -Verb RunAs in another terminal.
  • Wildcards not matching - Errors like No matching files found occur when wildcards don’t match. Debug: - Use exact filenames (e.g., ls "Report_2024.pdf"). - Add -ErrorAction Stop to halt on errors (e.g., ls *.docx -ErrorAction Stop).
  • Case-sensitive filtering - On Windows, ls *.TXT may fail if directory names are lowercase. Use: ls | Where-Object { $_.Name -like "*txt*" -i } for case-insensitive matches.
  • Overwriting files - Exporting with Out-File can silently overwrite existing files. Always append: ls | Out-File -FilePath "C:\output\file_list.txt" -Append.

Tips

  • Speed up repeated queries - Store common paths as variables: $home = "C:\Users\YourName"; ls -Path "$home\Documents"
  • Use aliases for quick navigation - Common aliases in PowerShell:
    • ls = Get-ChildItem
    • gci = Get-ChildItem
    • ls2 = Get-ChildItem -Force
    • dir = Get-ChildItem (legacy cmd)
  • Filter by size - Find large files: ls | Where-Object { $_.Length -gt 1MB } Convert KB to MB with: ls | Select-Object Name, @{Name="Size (MB)"; Expression={[math]::Round($_.Length/1MB,2)} }
  • Audit permissions - Check if a file is readable/writable: ls -File | Select-Object Name, @{Name="Read"; Expression={$_.PSIsContainer ? "Yes" : ($_.Attributes -match "R") ?

Troubleshooting

  1. PowerShell doesn’t recognize ls - The ls alias is part of the PowerShell 3.0+ default. If missing: - Reinstall PowerShell or run: Set-Alias ls Get-ChildItem (requires admin rights). - Check your version with powershell -version and update if below 5.1.
  2. Command returns nothing - Verify you’re in the correct directory with pwd (Print Working Directory). - List the entire directory tree with ls -Recurse to confirm files exist.
  3. Access denied on files - Try these fixes:
    1. Run PowerShell as administrator.
    2. Grant permissions via Windows Explorer:
      1. Right-click the file/folder > Properties > Security tab.
      2. Click Edit > Add your user/group > Check box > OK > Apply.
    3. Use ls -Force to bypass read-only flags.
  4. Performance lag in large directories - PowerShell uses depth-first traversal. For large folders: ls -Path "C:\BigFolder" -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Name -Sum (avoids hitting memory limits).
  5. Output appears blank after export - Check the target file path. Use ls alone first to validate the output: ls | Select-Object Name | Out-File "C:\output\test.txt"

Frequently Asked Questions

Why does ls show different results than dir?

dir (directory listing) is a legacy cmdlet from cmd.exe, while Get-ChildItem is a full-fledged PowerShell cmdlet that includes path resolution, recursive traversal, and advanced filtering. For example, ls -Recurse *.txt will find all .txt files in subfolders, whereas dir /s *.txt does the same but is more limited in PowerShell.

Can I use ls on Linux/macOS?

Yes! PowerShell 7+ runs natively on Linux (Ubuntu/Debian) and macOS. The command behaves identically: ls -a --color lists all files (including hidden ones) with color output. For macOS, add --no-newline to avoid trailing newlines in scripts.

How do I list only modified files from the last 7 days?

Use a pipeline with Where-Object and Get-Date: ls | Where-Object { $_.LastWriteTime -ge (Get-Date).AddDays(-7) }

What’s the difference between ls and Get-ChildItem?

Get-ChildItem is the cmdlet; ls is its alias. Both return the same output but Get-ChildItem is more versatile for scripting (e.g., supports -ErrorAction, -FileSystemInfo, or custom properties).

How do I list files with PowerShell but not show hidden folders?

Hide hidden folders by excluding the . and .. entries: ls | Where-Object { $_.Name -notmatch (".|..") } Or use -Force with a filter: ls -Force *.txt -Exclude ".*"

Can ls list network drives?

Yes, but you need to map the network drive first. After mapping (e.g., net use Z: \\server\share), use: ls -Path "Z:\" For unmapped drives, use the full UNC path: ls -Path "\\server\share"