PowerShell: How to Use Get-ChildItem to Return Files Only


By default, the Get-ChildItem cmdlet in PowerShell returns both folders and files in a particular directory.

However, you can use the -File operator to specify that you would only like to get the files and not the folders in a particular directory:

Get-ChildItem c:\users\bobbi\current_data -Recurse -File

This particular example will return only the files located in the following directory:

  • c:\users\bobbi\current_data

Also note that you could use the gci alias for Get-ChildItem if you’d like even shorter syntax:

gci c:\users\bobbi\current_data -Recurse -File

The following example shows how to use this syntax in practice.

Example: Use Get-ChildItem to Return Files Only in PowerShell

Suppose we have a folder named current_data saved in the following location:

  • c:\users\bobbi\current_data

Suppose that we use the Get-ChildItem cmdlet to return all folders and files in this directory:

Get-ChildItem c:\users\bobbi\current_data -Recurse

The following screenshot shows how to use this syntax in practice:

We can see that this directory contains two folders:

  • data2022
  • data2023

It also contains five files:

  • baseball_data.xlsx
  • basketball_data.xlsx
  • football_data.xlsx
  • hockey_data.xlsx
  • new_hockey_data.xlsx

Suppose that we would like to only return the files and not the folders.

We can use the following syntax to do so:

Get-ChildItem c:\users\bobbi\current_data -Recurse -File

The following screenshot shows how to use this syntax in practice:

PowerShell Get-ChildItem return files only

Notice that only the files are returned this time and not the folders.

We could also use the gci alias for Get-ChildItem to get the same results:

gci c:\users\bobbi\current_data -Recurse -File

The following screenshot shows how to use this syntax in practice:

Once again, only the files are returned and not the folders.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Get-ChildItem with Filter
PowerShell: How to Use Get-ChildItem to Get Full Path of Files
PowerShell: How to List Files in Directory by Date

Leave a Reply