How to Get All Files in a Directory Using PowerShell?

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" -File

In 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" -File

After I executed the above PowerShell script, it displayed a list of files from the Downloads folder, as shown below.

Get All Files in a Directory Using PowerShell

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 *.csv

This 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 *.csv

You can see the screenshot below, it lists only the CSV files.

PowerShell Get All Files in a Directory

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 -Recurse

This 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, CreationTime

This 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.