Understanding Get-ChildItem Command Basics


Get-ChildItem is one of the most versatile and useful commands in PowerShell for managing a file system. As its name suggests, it allows us to get items and child items in one or more specified locations. We can use this command to search for specific files or folders, to create a list of just folders (or just files), and even to filter the results according to any number of different criteria. By default, Get-ChildItem retrieves only the top-level child items. However, you can specify the -Recurse parameter to include all child containers in the search.

Get-ChildItem works with any PowerShell provider, not just the file system provider. We can use this command to manage items in any provider namespace, including the registry hive and certificate store. As a result, if you are not already familiar with this incredibly flexible and useful command, I believe you will find the time and effort spent learning Get-ChildItem to be very rewarding.

Syntax of Get-ChildItem

The basic syntax for Get-ChildItem is as follows:Get-ChildItem [[-Path] ] [[-Filter] ] [-Include ] [-Exclude ] [-Recurse] [-Depth ] [-Force] [-Name] [-UseTransaction] []

Utilizing the -Depth Parameter for Precise Directory Traversal

The -Depth parameter is a valuable tool for precisely traversing directories within a file system. By specifying the depth level, you can control how far down the directory hierarchy Get-ChildItem should search. This parameter allows you to filter the results to the desired level and retrieve information from specific subdirectories, optimizing the efficiency of your PowerShell scripts.

To illustrate its usage, consider the following example:

Get-ChildItem -Path C:\Files -Recurse -Depth 2

The above command will traverse through the “C:\Files” directory and retrieve items up to a depth level of 2, including files and subdirectories within that range.

Filtering Results with the -Include and -Exclude Parameters

The -Include and -Exclude parameters are invaluable when you want to filter results based on specific criteria. These parameters enable you to narrow down the items returned by Get-ChildItem by including or excluding certain files or directories that match specific patterns or attributes.

For example, consider the following commands:

  1. Get-ChildItem -Path C:\Files -Include *.txt
  2. Get-ChildItem -Path C:\Files -Exclude ReadMe.txt

The first command will retrieve all the files with the “.txt” extension within the “C:\Files” directory, while the second command will exclude the file named “ReadMe.txt” from the results.

Leveraging the -Attributes Parameter for Advanced Filtering

The -Attributes parameter provides advanced filtering options based on file attributes. With this parameter, you can select specific files or directories to include or exclude from the results based on their attributes, such as hidden, read-only, or system files.

Here’s an example of how the -Attributes parameter can be used:

Get-ChildItem -Path C:\Files -File -Attributes !Hidden

The above command will retrieve only the files that are not hidden within the “C:\Files” directory, excluding any hidden files from the results.

By understanding and utilizing these parameters effectively, you can tailor the behavior of Get-ChildItem to meet your specific requirements, enabling efficient file managementdirectory traversal, and result filtering.

PowerShell Scripting with Get-ChildItem

One common use case for Get-ChildItem is performing a file search. By using the -Filter parameter, you can search for files based on specific criteria such as file extension, size, or last modified date. For example, to find all text files in a directory, you can run the following command:

Get-ChildItem -Path C:\MyFiles -Filter *.txt

This command will return a list of all text files located in the “C:\MyFiles” directory. You can then manipulate this list as needed, whether it’s copying, moving, or deleting the files..

Counting Files in a Folder.

To count the number of files in a folder you might use:

Get-ChildItem -Path "C:/FolderPath" -File | Measure-Object | Select-Object -ExpandProperty Count

In this case we’re counting only the files (not directories) then using “Measure-Object” to find out how many items were passed to it, then using “Select-Object” to extract just the number of items counted.

Displaying Full File Paths.

If you want to display the full paths of files you instead might use:

Get-ChildItem -Path "C:/FolderPath" -File | Select-Object -ExpandProperty FullName

In this case I haven’t piped to a “Select-Object” to select any specific properties but rather the “-ExpandProperty” flag that tells “Select-Object” to “Expand” the “FullName” property so only that is shown.

Again, don’t be concerned if some of these parts are confusing. You can accomplish a lot even with the basic use of “Get-ChildItem” and naturally as your PowerShell skills progress you’ll very likely start to appreciate the ease with which you can pass output from one command to the next and to extract exactly what you need.

Retrieving Only Files.

Should you want to instead retrieve only the files (so that you can work with them further in a script for example) you might use:

Get-ChildItem -Path "C:/FolderPath" -File

The “-File” flag on the end is all that’s required to instruct “Get-ChildItem” to only retrieve files and no directories are possessed of a “-File” parameter.

Getting Full File Paths or Just File Names.

If you want only the full paths or the file names instead you might use:

Get-ChildItem -Path "C:/FolderPath" -File | Select-Object -ExpandProperty FullName

These examples demonstrate how to retrieve either the full paths or just the file names of the files in the specified folder.

Displaying Only Folders

To display only the folders in a directory, use the following command:

Get-ChildItem -Path "C:/FolderPath" -Directory

This example returns only the folders in the specified directory.

Understanding Get-ChildItem Command Basics 3

These examples provide specific use cases for utilizing the Get-ChildItem cmdlet. By following these examples, you can gain a better understanding of how to effectively use Get-ChildItem in your own PowerShell scripts and commands.

Navigating File Systems

Cleaning Up Old Log Files

  • Overview: Automate the process of identifying and deleting log files older than a specified date.
  • Usage Example:
$days = 30 $target

Folder = "C:\Logs" Get-ChildItem -Path $targetFolder -File -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$days) } | Remove-Item -Force

Automated Backups

  • Overview: Create scripts to back up specific file types or directories, excluding certain patterns.
  • Usage Example:
$sourceFolder = "C:\Data"
$backupFolder = "D:\Backup"
$date = Get-Date -Format "yyyyMMdd"
Get-ChildItem -Path $sourceFolder -Include *.docx,*.xlsx -Exclude *temp* -Recurse | Copy-Item -Destination {$backupFolder + "\" + $_.Name + "_backup_" + $date} -Force

Registry and Configuration Management

Auditing Registry Settings

  • Overview: Navigate the registry for compliance checks or settings review.
  • Usage Example:
$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" Get-ChildItem -Path $registryPath

Bulk Updating Application Settings

  • Overview: Modify configuration files across multiple servers or locations in a consistent manner.
  • Usage Example:
$configFiles = Get-ChildItem -Path "\\Server\Share\Configs" -Filter *.config -Recurse
foreach ($file in $configFiles) {
    # Example modification: changing a setting value
    (Get-Content $file.PSPath) | ForEach-Object { $_ -replace "setting1=value1", "setting1=newValue" } | Set-Content $file.PSPath

Security and Permissions Auditing

Finding Files with Insecure Permissions

  • Overview: Identify files accessible by non-administrative users that may compromise security.
  • Usage Example:
$folderPath = "C:\SensitiveData"
Get-ChildItem -Path $folderPath -File -Recurse | ForEach-Object {
    $acl = Get-Acl $_.FullName
    $acl.Access | Where-Object { $_.FileSystemRights -like "*FullControl*" -and $_.IdentityReference -notlike "*Administrators*" }

Data Analysis and Reporting

Generating Inventory Reports

  • Overview: Produce detailed reports on file inventories, including attributes like size, type, and modification dates.
  • Usage Example:
$directoryPath = "C:\Data" 
Get-ChildItem -Path $directoryPath -File -Recurse | Select-Object Name, Length, Extension, LastWriteTime | Export-Csv -Path "C:\Reports\inventory_report.csv" -NoTypeInformation

Enhancing Script Performance

Optimizing Large Directory Searches

  • Overview: Efficient searching in directories with a vast number of files or nested structures.
  • Usage Discussion: Limiting the depth of recursion and specifying file types can significantly improve performance.
  • Example Command:
Get-ChildItem -Path "C:\Data" -File -Recurse -Depth 2 -Filter *.log

Handling Special File Types

Managing Symbolic Links and Hard Links

  • Overview: Identify and differentiate symbolic links, hard links, and junction points in file systems.
  • Usage Example:
Get-ChildItem -Path "C:\Data" -Recurse | Where-Object { $_.Attributes -match "ReparsePoint" }

Script Organization and Best Practices

Structuring Scripts for Maintainability

  • Overview: Organize PowerShell scripts for easier maintenance and readability.
  • Usage Example:
function Backup-Files {
    param (
    Get-ChildItem -Path $sourceFolder -File -Recurse | Copy-Item -Destination {$backupFolder + "\" + $_.Name} -Force
Backup-Files -sourceFolder "C:\Data" -backupFolder "D:\Backup"

Error Handling and Debugging

Techniques for robust error handling.

  • Overview: Implement error handling in scripts.
  • Usage Example:
try { $path = "C:\NonExistentFolder" Get-ChildItem -Path $path -ErrorAction Stop } catch { Write-Error "An error occurred: $_" }

Retrieving Data from Registry and Certificate Store

To demonstrate the usage of Get-ChildItem for retrieving data from the registry and certificate store, consider the following examples:

  1. Retrieve all subkeys of a specific registry key:
    Get-ChildItem -Path 'Registry::HKEY_LOCAL_MACHINE\Software'
  2. Retrieve all certificates in the personal certificate store:
    Get-ChildItem -Path 'Cert:CurrentUser\My'

By customizing the path parameter in the Get-ChildItem command, you can fetch specific data from the registry or certificate store according to your requirements.

Example: Retrieving Registry Keys for Installed Programs

I often use Get-ChildItem to retrieve the registry keys for installed programs on a Windows system. By specifying the appropriate path, such as Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall*, I can quickly retrieve information about installed software. It helps me keep track of installed programs, uninstall unnecessary software when needed, and troubleshoot issues related to specific software installations.

Using Get-ChildItem in combination with other PowerShell cmdlets, like foreach or Where-Object, allows you to further filter and process the retrieved data from the registry or certificate store based on specific conditions.

Registry KeyDescription
Retrieves registry keys for installed programs
Registry::HKEY_CURRENT_USER\Software*Retrieves registry keys under the current user’s software section
Retrieves registry keys for cryptography providers


Understanding the basics of the Get-ChildItem command is essential for effective file system management in PowerShell. This powerful command allows you to retrieve items and child items from specified locations, search for specific files or folders, and filter results based on various criteria. By familiarizing yourself with the syntax and parameters of Get-ChildItem, you can customize your search and retrieve specific items that meet your needs.


What is the Get-ChildItem command in PowerShell?

The Get-ChildItem command is a powerful command in PowerShell that allows you to obtain the items and child items in one or more specified locations.

What can I do with the Get-ChildItem command?

With the Get-ChildItem command, you can traverse directories, search for specific files or folders, and filter results based on various criteria.

What are some important parameters of the Get-ChildItem command?

Some important parameters of the Get-ChildItem command include -Path, -Filter, -Include, -Exclude, -Recurse, and more.

How can I customize my search using the Get-ChildItem command?

You can customize your search using parameters such as -Path, -Filter, -Include, -Exclude, -Recurse, and -Attributes, which allow you to specify the location, apply filters, include or exclude specific items, traverse subdirectories, and retrieve items based on their attributes.

What are some practical examples of using the Get-ChildItem command?

Some practical examples include counting files in a folder, displaying full file paths, retrieving only files (excluding directories), getting full file paths or just file names, displaying only folders, and more.

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.