Get-ChildItem in PowerShell

As a PowerShell developer, you might use Get-ChildItem cmdlet in your daily life. In this tutorial, I will explain how to work with Get-ChildItem cmdlet in PowerShell.

To filter files larger than 1MB using PowerShell’s Get-ChildItem, you can combine it with Where-Object. For example, the command Get-ChildItem -Path "C:\Users\JohnDoe\Documents" -File | Where-Object { $_.Length -gt 1MB } will list all files in the specified directory that exceed 1MB in size.

Get-ChildItem in PowerShell

Get-ChildItem is a cmdlet in PowerShell that retrieves the items and child items in one or more specified locations. It can be used to list files, directories, registry keys, and certificates. This command is equivalent to the dir or ls command in other shells.

Syntax Of PowerShell Get-ChildItem cmdlet

The basic syntax for PowerShell Get-ChildItem is:

Get-ChildItem [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-Name] [-Force] [-Attributes <FileAttributes>] [-File] [-Directory] [-Hidden] [-System] [<CommonParameters>]

Here are the key parameters:

  • -Path: Specifies the path to the items.
  • -Filter: Filters the items based on a string.
  • -Include: Includes only items that match the specified pattern.
  • -Exclude: Excludes items that match the specified pattern.
  • -Recurse: Gets items from the specified location and all child locations.
  • -Name: Gets only the item names.
  • -Force: Gets hidden and system files.
  • -File: Gets only files.
  • -Directory: Gets only directories.
  • -Hidden: Gets only hidden items.
  • -System: Gets only system items.

Check out How to Add Date to Filename Using PowerShell?

PowerShell Get-ChildItem Examples

Let me show you some real examples of using the Get-ChildItem cmdlet in PowerShell.

Example 1: List Files and Directories

To list all files and directories in the “C:\MyFolder” directory, you can use the below PowerShell script.

Get-ChildItem -Path "C:\MyFolder"

This command will list all files and directories within the specified path.

Here is the output in the screenshot below:

powershell get-childitem

Example 2: Filter Files by Extension

If you want to list only .txt files in the “C:\MyFolder” directory, use:

Get-ChildItem -Path "C:\MyFolder" -Filter "*.txt"

This command is particularly useful when you need to focus on specific types of files. For instance, if you’re working on a project that involves text files, this command will help you quickly locate all .txt files in the specified directory. It saves time and makes file management more efficient.

Check out Sort Files by Date in PowerShell

Example 3: Recursively Listing Items

To list all items, including those in subdirectories, use the -Recurse parameter with the PowerShell Get-ChildItem like below:

Get-ChildItem -Path "C:\MyFolder" -Recurse

The -Recurse parameter is invaluable when you need to perform a deep search within a directory. It will list all items, including those in nested subdirectories.

Example 4: Get Only Directories

If you want to get only the directories, use the -Directory parameter with the Get-ChildItem.

Get-ChildItem -Path "C:\MyFolder" -Directory

This will display only the directories.

Here is the exact output in the screenshot below:

PowerShell Get-ChildItem cmdlet

Example 5: List Hidden Files

To list hidden files in a directory, include the -Hidden parameter with the Get-ChildItem in PowerShell.

Get-ChildItem -Path "C:\MyFolder" -Hidden

This command will list all hidden files within the specified directory. It’s particularly useful for troubleshooting issues or tasks requiring access to files that are not normally visible.

Check out Get the First 10 Files in a Folder Using PowerShell

Get-ChildItem Advanced Methods

Now, let me show you a few other advanced methods related to Get-ChildItem in PowerShell.

Method 1: Using Get-ChildItem with Where-Object

You can use Where-Object to filter items based on specific criteria. For example, to list files larger than 1MB:

Get-ChildItem -Path "C:\MyFolder" -File | Where-Object { $_.Length -gt 1MB }

This command combines Get-ChildItem with Where-Object to filter files based on their size. It’s particularly useful for identifying large files that may take up unnecessary space.

Method 2: Count Files

You can use the PowerShell cmdlets to count the number of files in a directory.

(Get-ChildItem -Path "C:\MyFolder" -File).Count

This command is useful for quickly counting files within a directory. Whether you’re conducting an inventory or need to know the number of files for organizational purposes, this command provides a fast and accurate count.

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

powershell get-childitem examples

Check out Get Unique Lines from a File Using PowerShell

Method 3: Sort Files by Date

To list files sorted by their last write time, you can write the below PowerShell script.

Get-ChildItem -Path "C:\MyFolder" -File | Sort-Object LastWriteTime

This command will list files in order of their last modification date, making it easy to track recent changes. Whether you’re monitoring project updates or conducting audits, this command provides valuable insights into file activity.

Method 4: Export File List to CSV

You can export the list of files to a CSV file for further analysis using the Get-ChildItem PowerShell cmdlets.

Get-ChildItem -Path "C:\MyFolder" -File | Export-Csv -Path "C:\MyFolder\FileList.csv" -NoTypeInformation

This command will create a CSV file containing the list of files, which can be opened in spreadsheet applications like Microsoft Excel. It’s particularly useful for generating reports, conducting audits, or sharing file information with others.

Method 5: Using Get-ChildItem with Select-Object

To get specific properties of files, use Select-Object with the Get-ChildItem cmdlets.

Get-ChildItem -Path "C:\MyFolder" -File | Select-Object Name, Length, LastWriteTime

This command allows you to select specific properties of files, such as their name, size, and last write time. It’s useful for generating customized views of file information.

Here is the exact output in the screenshot below:

get-childitem in powershell

Conclusion

In this tutorial, I explained how to work with the PowerShell Get-ChildItem cmdlet. I have explained a few examples of Get-ChildItem cmdlets.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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