One of my clients recently asked to list all files in a specific folder for auditing purposes. PowerShell provides different methods. In this tutorial, I will explain how to retrieve all files within a directory using PowerShell with examples.
Using Get-ChildItem to List Files in a Directory
The Get-ChildItem cmdlet in PowerShell allows you to list files and directories within a specified location. To get all files in a directory, you can use the following command:
Get-ChildItem -Path "C:\Users\Bijay\Documents" -FileIn this example, the -Path parameter specifies the directory path “C:\Users\Bijay\Documents”, and the -File parameter filters the results to include only files, excluding directories.
Since I am using a macOS, I have given the file path in a different way, like below:
Get-ChildItem -Path "/Users/bijay/Downloads" -FileAfter I executed the above PowerShell script, it displayed a list of files from the Downloads folder, as shown below.

Check out How to List Directories in PowerShell?
Filter Files by Extension
If you need to retrieve files with a specific extension, you can utilize the -Filter parameter along with a wildcard (*) to match the desired file type. For instance, to get all CSV files in a directory, use the following command:
Get-ChildItem -Path "C:\Users\Bijay\Reports" -Filter *.csvThis command will list all files with the .csv extension in the “C:\Users\Bija\Reports” directory.
For the macOS, the command is like below:
Get-ChildItem -Path "/Users/bijay/Downloads" -Filter *.csvYou can see the screenshot below, it lists only the CSV files.

Read Create a File in the Current Directory Using PowerShell
Retrieve Files Recursively
By default, Get-ChildItem only retrieves files from the specified directory. However, if you need to search for files in subfolders as well, you can use the -Recurse parameter. Here’s an example:
Get-ChildItem -Path "C:\Projects" -File -RecurseThis command will recursively traverse all subfolders within the “C:\Projects” directory and return a list of all files found.
Outputting File Details
In addition to the file names, you can also retrieve additional properties of the files using Get-ChildItem. By default, it displays the mode (attributes), last write time, file size, and name. However, you can customize the output using the -Properties parameter. For example:
Get-ChildItem -Path "C:\Users\MichaelBrown\Documents" -File | Select-Object Name, Length, CreationTimeThis command retrieves all files in the “C:\Users\MichaelBrown\Documents” directory and selects only the Name, Length (file size), and CreationTime properties for each file.
Read Read a Text File in PowerShell and Skip the First Line
Saving File List to a Variable or File
If you want to store the list of files for further processing or save it to a file, you can assign the output of Get-ChildItem to a variable or use the Out-File cmdlet. Here’s an example:
$files = Get-ChildItem -Path "C:\Users\SarahJohnson\Pictures" -File
$files | Out-File -FilePath "C:\Temp\PictureFiles.txt"In this example, the list of files is stored in the $files variable, and then the contents of the variable are written to a text file named “PictureFiles.txt” in the “C:\Temp” directory.
Conclusion
PowerShell’s Get-ChildItem cmdlet is used to retrieve all files within a directory. You can use various parameters, such as -File, -Filter, -Recurse, and -Properties to customize the output. I hope this tutorial helps you to know how to get all files in a directory using PowerShell.
You may also like:
- Create a File with Date in the Name Using PowerShell
- Create Folders with Year, Month, and Day Using PowerShell
- Create Files with Content Using PowerShell
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.