As a PowerShell user since its early days, I have been using the Get-ChildItem cmdlet to list files and directories. In this tutorial, I will explain different methods to list directories and files in PowerShell, from basic commands to advanced filtering techniques that I’ve used.
Get-ChildItem Cmdlet
The Get-ChildItem cmdlet (which has aliases like dir and ls) is the primary way to list files and directories in PowerShell. It’s similar to the dir command in CMD or ls in Unix/Linux. This cmdlet allows you to retrieve information about the contents of a specified directory.
Here’s how to use it in its simplest form:
Get-ChildItemThis command lists all non-hidden files and directories in your current location.
For example, suppose you have a folder called “Documents” on your C: drive containing various files and subfolders. To list the contents of this folder, you can use the following command:
Get-ChildItem -Path "C:\Documents"This will display a list of all the files and subdirectories within the “Documents” folder.
Check out Create a Folder with Today’s Date and Copy Files to it using PowerShell
List Files and Folders Recursively in PowerShell
In many cases, you may need to list files and folders not only in the specified directory but also in its subdirectories. PowerShell makes this easy with the -Recurse parameter. Recursively listing all files in a directory and its subdirectories is a common task for PowerShell developers.
Let’s say you have a folder structure like this:
C:\Projects
├── Project1
│ ├── Documents
│ └── Scripts
└── Project2
├── Designs
└── ReportsTo list all the files and folders within the “Projects” directory and its subdirectories, use the following command:
Get-ChildItem -Path "C:\Projects" -RecurseThis will display a hierarchical view of all the files and folders, including those in the “Project1” and “Project2” subdirectories.
You can see the exact output in the screenshot below:

Check out Delete User Profiles Older Than 30 Days Using PowerShell
Filter Files Recursively in PowerShell
If you want to list only files with a specific extension, you can use the -Filter parameter. For instance, to list only PDF files in the “Reports” folder, use this command:
Get-ChildItem -Path "C:\Projects\Project2\Reports" -Filter "*.pdf"This will display only the files with the “.pdf” extension in the specified folder.
However, you might need to list files from PowerShell’s folders and subfolders recursively.
To make the script recursive, you simply need to add the -Recurse parameter to the command. This will make PowerShell search through all subdirectories within the specified path:
Get-ChildItem -Path "C:\Projects\Project2\Reports" -Filter "*.pdf" -RecurseThis command will search recursively through all subfolders of the Reports directory and return all PDF files it finds.
If you want to ensure you’re only getting files (not directories that might match the pattern), you can add the -File parameter:
Get-ChildItem -Path "C:\Projects\Project2\Reports" -Filter "*.pdf" -Recurse -FileThis is particularly useful when dealing with many files across many subdirectories.
Here is another example to filter only PDF files recursively using PowerShell.
Get-ChildItem -Path "C:\Projects\" -Filter "*.pdf" -RecurseThis will recursively list all the PDF files from folders and subfolders using PowerShell. You can see the exact output in the screenshot below:

Check out Create Files with Content Using PowerShell
Format Output as a Table
By default, Get-ChildItem displays the results in a list format. However, you can format the output as a table for better readability using the Format-Table cmdlet. PowerShell makes it easy to list all of a directory’s folders and calculate the size of their contents.
For example, to display the name, last write time, and size of each file in the “Documents” folder as a table, use this command:
Get-ChildItem -Path "C:\Projects" -Recurse | Format-Table -Property Name, LastWriteTime, LengthThis will output a table with columns for the file name, last modified timestamp, and file size.
You can see the exact output in the screenshot below:

Conclusion
In this tutorial, we explored how to use PowerShell to list directories and files efficiently. We covered the Get-ChildItem cmdlet, recursive listing, filtering by file extension, and formatting output as a table.
You may also like:
- Get All Files in a Directory Using PowerShell
- Create a File in the Current Directory Using PowerShell
- Read a Text File in PowerShell and Skip the First Line
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.