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-ChildItemcmdlet 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
- Open PowerShell
- Windows 10/11: Press
Win+X, selectWindows 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. - Check basic directory contents
Run this command in the current directory to see all files and folders:
lsThis shows: - File names with icons (if PowerShell Desktop is installed). - File sizes (e.g., 12K for 12KB). - Last modified date. - Permission flags (e.g.,Rfor read,Wfor 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 MyDocumentsIf you seeAccess Denied, try running PowerShell as administrator or use:ls -Forceto bypass read-only checks. - List files by extension
Filter results with wildcards:
ls *.txtlists all .txt files.ls *.log | Sort-Object LastWriteTime -Descendingsorts logs by recency. For hidden files (e.g., `.config`), add-Force:ls -Force *.hidden. - Recursively list subdirectories
To explore folders deeply, use:
ls -Recurseor specify a path:ls -Path "C:\Project\Subfolder" -Recurse. For large directories, add-Depth 1to limit depth:ls -Depth 1 -Path "C:\" | Where-Object { $_.Name -ne "System" }(avoids the hidden Windows system folder). - Export results to a file
Save the output to CSV or a text file:
ls | Export-Csv -Path "C:\output\files.csv" -NoTypeInformationFor a plain text list:ls | Out-File -FilePath "C:\output\file_list.txt" - Use with aliases and parameters
Common shortcuts and flags:
ls -FullNameshows full paths (e.g.,C:\Temp\file.txt).ls -ErrorAction SilentlyContinueskips errors (e.g., inaccessible files).ls -TimeStampforces date updates (useful for scripts).ls * | Select-Object Name, Size, LastWriteTimecustomizes output columns.
gci -Recurse -Force | Where-Object { $_.Name -like "*test*" }(this usesgcialias forGet-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, useStart-Process PowerShell -Verb RunAsin another terminal. - Wildcards not matching
- Errors like
No matching files foundoccur when wildcards don’t match. Debug: - Use exact filenames (e.g.,ls "Report_2024.pdf"). - Add-ErrorAction Stopto halt on errors (e.g.,ls *.docx -ErrorAction Stop). - Case-sensitive filtering
- On Windows,
ls *.TXTmay fail if directory names are lowercase. Use:ls | Where-Object { $_.Name -like "*txt*" -i }for case-insensitive matches. - Overwriting files
- Exporting with
Out-Filecan 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-ChildItemgci=Get-ChildItemls2=Get-ChildItem -Forcedir=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
- PowerShell doesn’t recognize
ls- Thelsalias 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 withpowershell -versionand update if below 5.1. - Command returns nothing
- Verify you’re in the correct directory with
pwd(Print Working Directory). - List the entire directory tree withls -Recurseto confirm files exist. - Access denied on files
- Try these fixes:
- Run PowerShell as administrator.
- Grant permissions via Windows Explorer:
- Right-click the file/folder > Properties > Security tab.
- Click Edit > Add your user/group > Check box > OK > Apply.
- Use
ls -Forceto bypass read-only flags.
- 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). - Output appears blank after export
- Check the target file path. Use
lsalone 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"