Use GCI with Regular Expressions in PowerShell [Complete Guide]
PowerShell is a powerful command-line tool that helps you manage files, folders, system settings, and everyday Windows tasks. If you’ve ever needed to find files with specific names, search through folders, or filter files based on a naming pattern, PowerShell can make the job much easier.
One command you’ll frequently use is Get-ChildItem, commonly shortened to GCI. It lets you list files and folders in a directory. When you combine GCI with regular expressions (regex), you can filter file names using more flexible patterns than ordinary text matching allows.
For example, you can find files whose names contain numbers, identify files that end with a particular extension, or locate documents that follow a specific naming format.
In this guide, you’ll learn how to use GCI with regular expressions in PowerShell, starting with the basics and moving on to practical examples.
Use GCI with Regular Expressions in PowerShell

To use GCI with regular expressions in PowerShell, run Get-ChildItem to retrieve files and folders, then filter their names with the -match operator. For example, Get-ChildItem -File | Where-Object { $_.Name -match ‘\.txt$’ } lists files ending in .txt in the current directory.
GCI itself does not have a regular expression parameter for filtering file names. Instead, you pipe its results to Where-Object and use the -match operator to check each item’s name against a regex pattern.
Let’s walk through the process step by step.
Step 1: Understand the Get-ChildItem (GCI) Command
Before using regular expressions, it’s helpful to understand what GCI does.
The Get-ChildItem command lists the contents of a directory. It can show files, folders, or both. You can also use its shorter alias, gci, to run the same command with less typing.
Open PowerShell on your Windows computer. Click the Start menu, type PowerShell, and open Windows PowerShell or PowerShell 7.
To list the contents of your current directory, run:
Get-ChildItem
Press Enter, and PowerShell will display the available files and folders.
You can also list the contents of a specific directory:
Get-ChildItem -Path “C:\Users\YourName\Documents”
Replace YourName with your actual Windows username.
To display only files, use the -File parameter:
Get-ChildItem -File
To display only directories, use:
Get-ChildItem -Directory
These commands give you the starting point for working with GCI. The next step is learning how regular expressions help you narrow down the results.
Step 2: Understand Regular Expressions in PowerShell
A regular expression, also called regex, is a pattern used to match text. Instead of searching for an exact filename, you can describe the kind of text you want to find.
For example, suppose your Documents folder contains report.txt, notes.txt, photo.jpg, and budget.xlsx. You want to find only files ending in .txt.
The regex pattern for this is:
‘\.txt$’
Here, the backslash escapes the period so it matches a literal dot rather than the regex wildcard. The dollar sign means the match must occur at the end of the string.
PowerShell uses the -match operator to compare text against a regex pattern. For example:
“report.txt” -match ‘\.txt$’
The result is True because the filename ends in .txt.
However, this expression returns False:
“photo.jpg” -match ‘\.txt$’
That’s because the filename doesn’t end in .txt.
Understanding this simple example will help you build more useful patterns for searching files.
Step 3: Use GCI with the -match Operator
Now it’s time to combine GCI with regex.
The Where-Object command filters objects based on a condition. When you use it with GCI, PowerShell checks each file or folder and returns only the items that match your pattern.
For example, run:
Get-ChildItem -File | Where-Object { $_.Name -match ‘\.txt$’ }
This command lists files in the current directory whose names end in .txt.
Here’s how it works. First, Get-ChildItem -File retrieves files from the current directory. The pipe symbol (|) sends those files to Where-Object. The expression $_.Name refers to the name of the current file being checked. Finally, -match ‘\.txt$’ keeps only files whose names end in .txt.
You can also use the shorter GCI alias:
gci -File | Where-Object { $_.Name -match ‘\.txt$’ }
Both commands perform the same task.
Important: PowerShell’s -match operator is case-insensitive by default. This means it normally matches .txt, .TXT, and .Txt. If you need a case-sensitive match, use -cmatch instead.
Step 4: Find Files Using Different Regex Patterns
Once you understand the basic syntax, you can create patterns for different file-search tasks.
Suppose you want to find files ending in either .txt or .log. Use the following command:
Get-ChildItem -File | Where-Object { $_.Name -match ‘\.(txt|log)$’ }
The parentheses group the alternatives, and the vertical bar means “or.” The dollar sign ensures that the extension appears at the end of the filename.
If you want to find files whose names contain a number, try:
Get-ChildItem -File | Where-Object { $_.Name -match ‘\d’ }
The \d pattern matches a digit from 0 through 9. This command returns filenames such as report2026.txt or image5.png.
You can also find files whose names begin with backup:
Get-ChildItem -File | Where-Object { $_.Name -match ‘^backup’ }
The caret (^) matches the beginning of the filename.
These examples demonstrate why regex is useful. You can search for a particular extension, a number, or a naming pattern without writing a separate command for every possible filename.
Step 5: Search Subfolders with GCI and Regex
Sometimes the files you’re looking for aren’t stored directly in the current folder. They may be scattered across multiple subfolders.
In that situation, use the -Recurse parameter.
For example:
Get-ChildItem -Path “C:\Reports” -File -Recurse |
Where-Object { $_.Name -match ‘\.pdf$’ }
This command searches the C:\Reports directory and its subfolders for files ending in .pdf.
The -Path parameter identifies the starting directory, -File limits the results to files, and -Recurse tells PowerShell to search through subdirectories.
You can change the regex to find other file types. For example, replace ‘\.pdf$’ with ‘\.(docx|xlsx)$’ to find Word documents and Excel workbooks.
Keep in mind that recursive searches can take longer when a directory contains thousands of files. You may also encounter access-denied errors when PowerShell reaches protected folders. Start with a directory you can access and expand the search only when needed.
Step 6: Use Regex to Match Specific File Naming Formats
Regular expressions become especially useful when filenames follow a predictable format.
Imagine you save reports using names such as Report_2025.txt, Report_2026.txt, and Report_2027.txt. You want to find reports containing a four-digit year.
Run this command:
Get-ChildItem -File | Where-Object { $_.Name -match ‘^Report_\d{4}\.txt$’ }
The pattern checks that the filename begins with Report_, contains exactly four digits, and ends with .txt.
The expression \d{4} means four consecutive digits. The escaped period matches the actual dot before the extension, while $ ensures that no additional characters appear after .txt.
If you want to match reports from 2025 through 2029 specifically, you can use:
Get-ChildItem -File | Where-Object { $_.Name -match ‘^Report_202[5-9]\.txt$’ }
The character range [5-9] matches one digit from 5 through 9.
This approach is useful for organizing dated reports, identifying numbered backups, and checking whether files follow your preferred naming convention.
Step 7: Combine Multiple Conditions in One Command
You may sometimes need to filter files based on more than one requirement.
For example, suppose you want to find .log files whose names contain a number. You can combine the conditions in a single expression:
Get-ChildItem -File | Where-Object {
$_.Name -match ‘\.log$’ -and $_.Name -match ‘\d’
}
The -and operator means both conditions must be true. As a result, PowerShell returns only files that end in .log and contain at least one digit somewhere in the filename.
If you want files ending in either .txt or .log, you can use:
Get-ChildItem -File | Where-Object {
$_.Name -match ‘\.(txt|log)$’
}
This second command uses regex alternation instead of a separate logical condition.
You can choose either approach depending on how complicated your requirements are. For more advanced searches, combining regex patterns with PowerShell comparison operators makes it easier to express the exact conditions you need.
Step 8: Save Matching File Names to a Text File
After filtering files with GCI and regex, you may want to save the results for later use.
For example, the following command finds .txt files in the current directory and exports their full paths to a text file:
Get-ChildItem -File | Where-Object {
$_.Name -match ‘\.txt$’
} | Select-Object -ExpandProperty FullName |
Set-Content -Path “C:\Temp\MatchingFiles.txt”
Make sure the C:\Temp directory exists before running this command. If it doesn’t, create the folder first or change the output path to a location that already exists.
The Select-Object -ExpandProperty FullName command extracts each matching file’s full path. Set-Content writes those paths to the output file.
Be careful when choosing the destination. If MatchingFiles.txt already exists, Set-Content replaces its existing contents.
If you only want to see matching filenames without saving them, you can skip the export portion and run the filtering command by itself.
FAQs
1. What does GCI mean in PowerShell?
GCI is the built-in alias for Get-ChildItem. It lists files and directories in a specified location. You can combine it with filtering commands to find items that meet specific requirements.
2. Does GCI support regular expressions directly?
GCI does not provide a dedicated regex parameter for matching filenames. The usual approach is to pipe its results to Where-Object and use the -match operator against properties such as Name or FullName.
3. What is the difference between -match and -like?
The -match operator uses regular expressions, which support patterns such as digits, character ranges, and alternatives. The -like operator uses wildcard patterns such as *.txt and report*. For simple filename filtering, wildcards may be easier to use.
4. How can I make a regex search case-sensitive?
Use the -cmatch operator instead of -match. For example, Get-ChildItem -File | Where-Object { $_.Name -cmatch ‘\.TXT$’ } matches filenames ending in uppercase .TXT, but not lowercase .txt.
5. Can I use GCI with regex to search subfolders?
Yes. Add -Recurse to the GCI command. For example, Get-ChildItem -Path “C:\Reports” -File -Recurse | Where-Object { $_.Name -match ‘\.pdf$’ } searches the specified directory and its subfolders for PDF files.
Summary
Using GCI with regular expressions in PowerShell gives you more control over how you search for files and folders. GCI retrieves directory contents, while Where-Object and the -match operator filter those results based on your regex pattern.
Start with simple patterns such as ‘\.txt$’, then experiment with digits, alternatives, character ranges, and filename formats. Add -Recurse when you need to search subfolders, and use Select-Object with Set-Content when you want to save the results.
Once you get comfortable with these techniques, you’ll be able to find files faster, manage large directories more efficiently, and automate many everyday file-search tasks in PowerShell.
