Using Get-ChildItem for Files Only in PowerShell – Example Script

Have you ever struggled to find a specific file in PowerShell? Searching through directories can be time-consuming, especially if you have a mix of files and folders. But what if there was a way to filter out directories and focus only on the files you need?

Introducing the Get-ChildItem command in PowerShell, a powerful tool for retrieving items and child items from specified locations. But did you know that you can use it to search for files only?

Today, I’ll show you how to use the Get-ChildItem command to find files only in PowerShell. Whether you’re a seasoned PowerShell user or just getting started, this technique can help expedite your file searches and streamline your file management tasks.

But first, let’s take a look at the basic command:

Get-ChildItem -File

This deceptively simple command provides the key to filtering out directories and listing only files. By adding the -File parameter, you can instruct PowerShell to return only the files in the specified location, excluding any directories.

Are you ready to dive deeper into using Get-ChildItem for files only? Let’s explore some additional techniques and options to enhance your file search experience.

Key Takeaways:

  • The Get-ChildItem command in PowerShell can be used to retrieve items and child items from specified locations.
  • By adding the -File parameter to the command, you can filter out directories and list only files.
  • Using the -Recurse parameter enables you to search for files in the current directory and its subdirectories.
  • The Where-Object cmdlet allows for more specific file filtering based on criteria you define.
  • Addit

Using Get-ChildItem Recursively to Find Files Only

To search for files only using the Get-ChildItem command recursively, you can add the -File parameter to the command. For example, Get-ChildItem -File -Recurse will search for files in the current directory and its subdirectories, excluding any directories from the returned results. This can be useful for simplifying file management tasks and focusing only on the files you need.

When using the Get-ChildItem -File -Recurse command, the output will include a list of files found throughout the specified directory and its subdirectories. This recursive search saves time and effort by automatically including all relevant files, making it easier to locate and work with the desired files.

Here is an example output from the Get-ChildItem -File -Recurse command:


In the table above, the output displays the path, name, and extension of each file found. This information can help you quickly identify the files you need and take appropriate actions, such as copying, moving, or deleting them.

By utilizing the recursive search capability of Get-ChildItem along with the -File parameter, you can efficiently find and manage files in your PowerShell scripts.

Filtering Files Using Get-ChildItem and Where-Object

When working with PowerShell’s Get-ChildItem cmdlet, there is another method to retrieve only files. By using the Where-Object cmdlet in conjunction with Get-ChildItem, you can filter the results and focus solely on files.

To implement this method, you can utilize the following command:

Get-ChildItem | Where-Object {!$_.PSIsContainer}

This command applies a filtering condition through the Where-Object cmdlet, specifically targeting items that are not containers. In this case, the only items returned will be files. By dynamically adjusting the criteria within Where-Object, you gain more flexibility to filter the results based on your specific needs.

This approach is particularly useful when you want to refine your file search and exclude any directories from the results. It allows you to focus solely on the files you require for your task at hand.

Let’s take a look at an example to illustrate this technique:

PS C:\Scripts> Get-ChildItem | Where-Object {!$_.PSIsContainer}

    Directory: C:\Scripts

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        12/9/2022   1:34 PM           1469 script1.ps1
-a----        12/9/2022   1:35 PM           1527 script2.ps1
-a----        12/9/2022   1:36 PM           1854 script3.ps1
-a----        12/9/2022   1:37 PM           1646 script4.ps1

In this example, the Get-ChildItem command is executed, and the Where-Object cmdlet filters out any containers. The result is a list of only the files present in the specified directory.

Using the combination of Get-ChildItem and Where-Object, you can effectively filter files and streamline your PowerShell file management tasks.

Filtering Files Using Get-ChildItem and Where-Object – Example Scenario:

Let’s say I have a directory that contains various types of files, such as text documents, images, and executables. However, I only need to work with the text files for a specific task. Here’s how I can achieve this using Get-ChildItem and Where-Object:

PS C:\Documents> Get-ChildItem | Where-Object {!$_.PSIsContainer -and $_.Extension -eq ".txt"}

    Directory: C:\Documents

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        12/9/2022   2:45 PM           1024 file1.txt
-a----        12/9/2022   2:49 PM           2048 file2.txt
-a----        12/9/2022   2:53 PM           3072 file3.txt

In this scenario, by adding an additional filter using `$_Extension -eq “.txt”`, the command will only return text files within the directory. This helps me quickly identify and work with the specific files I need for my task.

With this method, you have the flexibility to customize your file filtering criteria based on various properties such as extension, size, or any other relevant attributes.

Additional Options to Filter Files with Get-ChildItem

In addition to the -File parameter and the Where-Object cmdlet, Get-ChildItem offers other options to filter and search for files only. You can use parameters like -Include, -Exclude, and -Attributes to specify file types, exclude certain files, or filter based on file attributes. These options provide more control over the file search process.

Using the -Include Parameter

The -Include parameter allows you to specify file types or patterns to include in the search results. For example, to search for text files only, you can use the following command:

Get-ChildItem -Include *.txt

Using the -Exclude Parameter

The -Exclude parameter allows you to exclude specific files or patterns from the search results. For example, if you want to exclude all files with the extension “.xml”, you can use the following command:

Get-ChildItem -Exclude *.xml

Using the -Attributes Parameter

The -Attributes parameter enables you to filter files based on their attributes. You can specify one or more attributes to include or exclude from the search results. For example, to find read-only files, you can use the following command:

Get-ChildItem -Attributes ReadOnly

The available attributes include:

  • ReadOnly – Files that are read-only
  • Hidden – Files that are hidden
  • System – Files that are system files
  • Archive – Files that have the archive attribute

By utilizing these additional options, you can further refine your file search and easily find the specific files you need with greater precision.

Continue reading to learn more about understanding the output of Get-ChildItem for files only.

Understanding the Output of Get-ChildItem for Files Only

When using Get-ChildItem to retrieve files only, the command provides valuable information about each file. This information includes the mode (attributes) of the file, the last write time, file length, and the filename itself.

By understanding the output of Get-ChildItem, you can easily interpret the mode flags displayed for each file. These mode flags provide insights into the characteristics of the file. Here are the common mode flags and their interpretations:

Symbolic link

By identifying these mode flags, you gain valuable insights into the attributes and properties of each file. This understanding allows you to efficiently work with the files that meet your specific needs.

Let’s take a look at an example output of Get-ChildItem:

ModeLast Write TimeLengthName
a—2022-01-15 09:47 AM512 KBfile1.txt
—-2022-02-02 02:18 PM2.5 MBfile2.jpg
–h-2022-03-10 08:33 AM1.2 GBfile3.docx

Please note: The above table is purely for illustrative purposes and doesn’t reflect real data or information.

This table showcases the output of Get-ChildItem, including the mode flags, last write time, file length, and file names. With this output, you can easily identify the attributes of each file.

Understanding the output of Get-ChildItem empowers you to navigate and manage files effectively, saving time and effort in your PowerShell tasks.


By utilizing the Get-ChildItem cmdlet in PowerShell, specifically filtering for files only, you can simplify your file management tasks. Whether you need to search for files recursively, apply additional filters, or understand the output, Get-ChildItem provides the flexibility and control you need.

With the ability to search for files only using the -File parameter or by filtering with the Where-Object cmdlet, you can efficiently retrieve the files you need without the extraneous clutter of directories. Additionally, the Get-ChildItem cmdlet offers options like -Include, -Exclude, and -Attributes to further customize your file search, giving you more control over the process.

Understanding the output of Get-ChildItem for files only is also essential for effective file management. By interpreting the mode flags and utilizing the displayed information like last write time, file length, and name, you gain valuable insights into the files you are working with.

By employing these techniques, you can enhance your PowerShell scripting and streamline your file-related operations, making file management a breeze with the power of Get-ChildItem.

Nilesh Kamble is Certified in Microsoft & GCP, having 13+ Years of Experience in IT Industry. As a Senior IT Employee, having vast experience on Windows Server, Windows Client, Powershell, Cloud Technologies. Passionate about Laptop, Mobiles & Other emerging Technologies.