When you’re working with files and folders in Windows, finding a specific file among hundreds or even thousands of items can take time. Fortunately, PowerShell gives you commands that make file searching much easier. One useful combination is Get-ChildItem (GCI) and regular expressions (regex).
The Get-ChildItem command, commonly shortened to gci, lets you list files, folders, and other items in a directory. Regular expressions help you identify files based on specific text patterns, such as filenames that contain numbers, start with a particular word, or end with a certain extension.
For example, suppose you want to find every log file whose name starts with error and ends with a date. Instead of checking each file manually, you can use gci with the -match operator to filter filenames using a regex pattern.
In this guide, you’ll learn how to use GCI with regular expressions in PowerShell, including filtering filenames, searching subdirectories, combining conditions, and avoiding common mistakes.
How to Use GCI with Regular Expressions

- Open Windows PowerShell or Windows Terminal and select a PowerShell session.
- Use gci to list files in your target directory.
- Add Where-Object and the -match operator to filter filenames using a regular expression.
- Use -Recurse when you need to search through subdirectories.
- Test your regex pattern against sample filenames before using it for a large search.
For example, the following command finds files with names ending in .log:
gci -File | Where-Object { $_.Name -match ‘\.log$’ }
This command lists files in the current directory whose names end with .log. The regular expression \.log$ matches the literal .log extension at the end of a filename.
Step 1: Open PowerShell and Choose a Directory
Before using regular expressions, you need to open PowerShell and decide where you want to search. You can launch Windows Terminal from the Start menu and open a PowerShell tab. You can also search for Windows PowerShell directly.
Next, navigate to the folder containing the files you want to examine. For example, if your files are stored in C:\Logs, run the following command:
Set-Location “C:\Logs”
The Set-Location command changes your current working directory. Once you have moved into the correct folder, you can use gci without typing the full directory path every time.
To confirm that you’re in the right location, enter:
gci
PowerShell displays the items in the current directory. By default, the results can include both files and folders.
Navigation path: Start menu > Windows Terminal > PowerShell > Set-Location > gci
Step 2: Understand How GCI Works
The gci command is an alias for Get-ChildItem. It retrieves the child items inside a specified location, such as a directory.
For example:
gci “C:\Logs”
This command lists the items inside the C:\Logs folder. If you want to display files only, use the -File parameter:
gci “C:\Logs” -File
To display folders only, use:
gci “C:\Logs” -Directory
Understanding these parameters is important because GCI does not automatically interpret a regular expression as a filename filter. Its -Filter parameter generally uses simple wildcard patterns, such as *.log, rather than full regex syntax.
To apply regular expressions to the returned items, you’ll usually combine GCI with Where-Object and the -match operator.
Navigation path: PowerShell > gci > -File or -Directory > Review the returned items
Step 3: Filter Filenames Using the Match Operator
Now let’s use a regular expression to find files based on their names.
Suppose your directory contains these files:
error.log
error2026.log
report.txt
error_backup.log
You want to find files whose names start with error. Run this command:
gci -File | Where-Object { $_.Name -match ‘^error’ }
Here’s how it works. The pipe symbol (|) passes the results from GCI to Where-Object. The expression $_.Name refers to the name of the current file being examined, while -match checks whether that name matches the regex pattern.
The caret (^) means the beginning of a string. Therefore, ^error matches filenames that begin with error.
If you want to match filenames ending in .log, use:
gci -File | Where-Object { $_.Name -match ‘\.log$’ }
In this pattern, the backslash escapes the period so that it matches a literal dot. The dollar sign ($) means the end of the string.
Navigation path: PowerShell > gci -File > Where-Object > -match > Enter your regex pattern
Step 4: Find Files That Contain Numbers
Regular expressions become especially useful when filenames follow a naming convention.
Imagine that your folder contains files named report123.txt, report456.txt, and summary.txt. You want to find files whose names contain one or more digits.
Use this command:
gci -File | Where-Object { $_.Name -match ‘\d+’ }
The regex token \d matches a digit from 0 through 9. The plus sign (+) means one or more occurrences of the preceding token. Together, \d+ matches a sequence containing at least one digit.
You can also search for filenames that begin with report and contain digits before the .txt extension:
gci -File | Where-Object { $_.Name -match ‘^report\d+\.txt$’ }
This pattern requires the filename to start with report, followed by one or more digits, and then end with .txt. It matches report123.txt but not report_backup.txt.
Navigation path: PowerShell > GCI results > Where-Object > -match ‘\d+’ or your custom regex
Step 5: Search Subdirectories Recursively
Sometimes, the files you’re looking for aren’t stored directly in the main folder. They may be spread across several subdirectories.
To search the current directory and its subdirectories, add the -Recurse parameter:
gci “C:\Logs” -File -Recurse |
Where-Object { $_.Name -match ‘\.log$’ }
This command searches for files ending in .log throughout the directory tree under C:\Logs.
You can also use a full path to inspect the location of every matching file. For example:
gci “C:\Logs” -File -Recurse |
Where-Object { $_.Name -match ‘^error\d*\.log$’ } |
Select-Object FullName
Here, \d* matches zero or more digits, so the pattern can match both error.log and error123.log. The FullName property displays the complete path, helping you identify where each file is stored.
Keep in mind that recursive searches can take longer when a directory contains many files. You may also encounter access-denied messages in protected locations.
Navigation path: PowerShell > Specify the directory > -File -Recurse > Where-Object > Select-Object FullName
Step 6: Combine Multiple Regex Conditions
You can combine regular expressions with logical operators when you need more specific results.
Suppose you want files whose names start with error and end with either .log or .txt. You can use:
gci -File | Where-Object {
$_.Name -match ‘^error’ -and
$_.Name -match ‘\.(log|txt)$’
}
The -and operator requires both conditions to be true. The parentheses in \.(log|txt)$ create an alternative group, allowing either log or txt after the period.
You can also use a single regex pattern:
gci -File | Where-Object {
$_.Name -match ‘^error.*\.(log|txt)$’
}
In this expression, .* matches zero or more characters between error and the extension.
Remember that PowerShell’s -match operator is case-insensitive by default. If you need a case-sensitive match, use -cmatch instead.
Navigation path: PowerShell > GCI results > Where-Object > Combine regex patterns with -and or alternation
Step 7: Use Regex to Filter File Extensions
You can use regular expressions to search for multiple file types in one command.
For example, to find files ending in .log, .txt, or .csv, run:
gci -File | Where-Object {
$_.Extension -match ‘^\.(log|txt|csv)$’
}
The Extension property returns the file extension, including its leading period. The pattern matches one of the three listed extensions.
This approach is useful when you’re organizing reports, collecting log files, or reviewing documents with different formats.
If you only need a simple extension match, however, a wildcard filter may be easier:
gci -File -Filter “*.log”
Use regex when you need pattern-based flexibility, such as checking several extensions or applying more complex filename rules. Use -Filter when a simple wildcard is enough.
Navigation path: PowerShell > gci -File > Where-Object > Match the Extension property
Step 8: Test and Troubleshoot Your Regex
If your command returns no results, the problem may be your pattern, the folder path, or the files you’re searching.
Start by checking whether GCI can find the files at all:
gci “C:\Logs” -File
Next, test your regex against a known filename:
“error123.log” -match ‘^error\d+\.log$’
PowerShell returns True if the filename matches and False if it doesn’t.
You can also review the automatic match results stored in $Matches:
“error123.log” -match ‘^([a-z]+)(\d+)\.log$’
$Matches
The pattern uses capture groups to separate the text portion from the numeric portion. $Matches[1] contains error, while $Matches[2] contains 123.
One important detail: $Matches is populated by a successful scalar -match operation, but filtering multiple objects with Where-Object does not give you a separate, persistent $Matches result for every file.
Navigation path: PowerShell > Test a sample filename > Check True or False > Adjust the pattern > Run GCI again
FAQs
Can I use regex directly with GCI?
Not through the -Filter parameter, which uses wildcard-style filtering rather than full regular expressions. For regex matching, pipe GCI’s results into Where-Object and use -match or -cmatch.
What is the difference between -match and -like?
-match supports regular expressions, such as \d+ or ^error. The -like operator uses wildcard patterns, such as error* or *.log.
Is GCI the same as Get-ChildItem?
Yes. gci is a PowerShell alias for Get-ChildItem. You can use either command to retrieve directory contents.
How do I search for files recursively using regex?
Combine -Recurse with Where-Object. For example:
gci “C:\Logs” -File -Recurse |
Where-Object { $_.Name -match ‘\.log$’ }
Why does my regex match uppercase filenames?
PowerShell’s -match operator is case-insensitive by default. Use -cmatch when you need case-sensitive matching.
Final Words
Using GCI with regular expressions gives you more control over file searches in PowerShell. You can filter filenames by prefixes, extensions, numbers, and other patterns without manually reviewing every file.
The key is to understand how these commands work together. Use gci to retrieve files, Where-Object to examine each result, and -match to apply your regex pattern. Add -Recurse when you need to search subdirectories, and test complicated expressions against sample filenames before running a large search.
Once you’re comfortable with these techniques, you can build more precise PowerShell commands for everyday file management and troubleshooting.