0
0
PowershellHow-ToBeginner · 3 min read

How to List Files in PowerShell: Simple Commands and Examples

Use the Get-ChildItem cmdlet in PowerShell to list files in a directory. For example, Get-ChildItem lists all files and folders in the current directory, while Get-ChildItem -File lists only files.
📐

Syntax

The basic command to list files and folders is Get-ChildItem. You can add parameters to filter results:

  • -Path: Specify the folder to list files from.
  • -File: Show only files, excluding folders.
  • -Directory: Show only folders.
  • -Recurse: List items in all subfolders recursively.
powershell
Get-ChildItem [-Path <string>] [-File] [-Directory] [-Recurse]
💻

Example

This example lists all files in the current folder, showing only files (no folders):

powershell
Get-ChildItem -File
Output
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2024-06-01 10:00 1234 example.txt -a---- 2024-05-30 15:45 5678 report.pdf -a---- 2024-06-02 09:20 4321 image.png
⚠️

Common Pitfalls

Beginners often forget that Get-ChildItem lists both files and folders by default, which can be confusing. To list only files, use the -File parameter. Also, specifying a path incorrectly or forgetting to use quotes for paths with spaces can cause errors.

powershell
Get-ChildItem C:\"My Documents"

# Correct way with quotes:
Get-ChildItem "C:\My Documents" -File
📊

Quick Reference

CommandDescription
Get-ChildItemLists files and folders in current directory
Get-ChildItem -FileLists only files
Get-ChildItem -DirectoryLists only folders
Get-ChildItem -RecurseLists all files and folders recursively
Get-ChildItem -Path Lists items in specified folder

Key Takeaways

Use Get-ChildItem to list files and folders in PowerShell.
Add -File to list only files, excluding folders.
Use quotes around paths with spaces to avoid errors.
Use -Recurse to list files in all subfolders.
By default, Get-ChildItem shows both files and folders.