Home » PowerShell » PowerShell Find file (Search for Files using Get-ChildItem)

PowerShell Find file (Search for Files using Get-ChildItem)

The Get-ChildItem cmdlet in PowerShell is used to get items in one or more specified locations. Using Get-ChildItem, you can find files. You can easily find files by name, and location, search file for string, or find file locations using a match pattern.

You can use the Get-ChildItem cmdlet in PowerShell to

  • Find files in a directory.
  • Search for files by an extension in the current directory.
  • Find files on the root drive.
  • Search for files recursively in the directory and subdirectories.
  • Search for files that don’t match a pattern.
  • List all files that match a pattern.
  • Search for files that match a specific pattern.
  • Find files file by name.
  • Find files by wildcard
  • Find files older than specific days.

In this article, I will explain different and best possible ways to find files that match a pattern or find files by extension in PowerShell

PowerShell Find File using the Get-ChildItem

Use the PowerShell Get-ChildItem cmdlet to show a list of files or directories in one or more locations.

The Get-ChildItem cmdlet provides more flexibility for simple or advanced wildcards to find files by a search pattern.

Using the Recurse parameter to get items recursively from all the child containers. You can limit the Depth parameter to limit the number of levels to recurse.

Cmdlet: Get-ChildItem

Syntax:

Get-ChildItem
   [[-Filter] <string>]
   -LiteralPath <string[]>
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Recurse]
   [-Depth <uint32>]
   [-Force]
   [-Name]
   [-Attributes <FlagsExpression[FileAttributes]>]
   [-FollowSymlink]
   [-Directory]
   [-File]
   [-Hidden]
   [-ReadOnly]
   [-System]
   [<CommonParameters>]

Let’s understand using PowerShell to find files by name, by extension, or find files recursively with different examples as given below.

Do you know: Using IIS to get a list of websites in PowerShell!

PowerShell Find files by extension in the current directory

To find all files by extension in the current directory that matches wildcard pattern *.txt

PS C:\Temp> Get-ChildItem *.txt

In the above script, the Get-ChildItem cmdlet finds files in the current directory that match extension .txt.

PowerShell Find All Files on the Root of drive D:\

To find and list all files stored on drive D:\ location, use the command PowerShell Get-ChildItem.

PS C:\> Get-ChildItem -Path D:\

The Get-ChildItem cmdlet uses the Path parameter to specify the location, in this case, D:\, and lists all the directories and files stored on location. It displays results items with Mode, LastWriteTime, and Length Name columns.

PowerShell Find File Recursively using Recurse parameter

To find and list all files stored on drive D:\ location, use PowerShell Get-ChildItem with Recurse parameter in PowerShell.

PS C:\> Get-ChildItem -Path D:\ -Recurse

In the above PowerShell script, the Get-ChildItem cmdlet uses the Recurse parameter to recursively get files from a location. This command will list all files that the user has access to, however, doing recurse operation, if the user doesn’t have access to any of the resource items, it will throw an error.

To continue with the Recurse operation even in the case of error, using ErrorAction parameter with silentlyContinue continue its operation.

PS C:\> Get-ChildItem -Path D:\ -Recurse -ErrorAction SilentlyContinue

PowerShell Tip: How to add a new line to a string or variable?

Search for files that do not match using the Exclude parameter

To find all files in the current directory that do not match PowerShell wildcard *.exe, we can do it using the Exclude parameter with the Get-ChildItem cmdlet.

PS D:\Temp> Get-ChildItem -Exclude *.exe -Recurse

Above command, get a list of all files exclude *.exe files in subdirectories using the recurse parameter in PowerShell.

PowerShell Tip: How to search strings in files using PowerShell Grep!

Get a List Of All Files in the Directory that Match a Pattern

To get a list of all files in the directory and subdirectory that matches PowerShell wildcard pattern *.doc,*.docx, use the Get-ChildItem cmdlet with the Include parameter.

PS D:\Temp> Get-ChildItem -Include *.doc,*.docx -File -Recurse -ErrorAction SilentlyContinue

In the above example, Get-ChildItem uses the Include parameter to find *.doc or *.docx files from the directory or its subdirectories using the Recurse parameter.

Use ErrorAction silentlyContinue to continue with finding files even without having errors.

Above command, search for files and get a list of all files in a directory in PowerShell.

Find File All Items in Subdirectories Match Specific Filter

Use the PowerShell Get-ChildItem command with the -Filter parameter to get a list of all files from subdirectories that match filter *.txt.

PS D:\Temp> Get-ChildItem -Filter *.txt -Recurse

The above Get-ChildItem command uses the Recurse parameter to recursively iterate in folders and subfolders and the Filter parameter with *.txt to get only *.txt file extension files only.

The above PowerShell script finds files recursively with extension.

Cool Tip: Replace text in a string using PowerShell!

PowerShell Find Filename Containing String

To find all files containing a string in a given directory or subdirectories, use the below command.

PS D:\Temp> Get-ChildItem -Recurse | Where {$_.Name -match 'Replace'} | Select Fullname

FullName                  
--------                  
D:\Temp\Replace-Method.txt

In the above example, Get-ChildItem uses the Recurse parameter to recursively find all files by name specified in the Where condition.

Using Where-Object cmdlet to compare name property that matches with ‘Replace‘ and returns FullName of the file.

In the above script, search the file for a string and get a file name that matches the string.

We can also use the Get-ChildItem alias GCI to query and file name containing a string as below.

PS D:\Temp> gci -Recurse | Where {$_.Name -match 'Replace'} | Select Fullname

FullName                  
--------                  
D:\Temp\Replace-Method.txt

PowerShell Find Files in Directory Containing a String

To find all files in the directory containing the string, use the below command

PS D:\Temp> Get-ChildItem -Recurse | Where {$_.DirectoryName -match 'Debug'} | Select Fullname

In the above example, Get-ChildItem uses the Recurse parameter to recursively find all files in the Directory

Using the Where-Object cmdlet to compare the DirectoryName property that matches with ‘Debug’ and returns the FullName of the files in that directory.

We can also use PowerShell Get-ChildItem alias GCI to query and list all files within the directory name containing a string as below.

PS D:\Temp> gci -Recurse | Where {$_.DirectoryName -match 'Debug'} | Select Fullname

PowerShell Tip: How to get MD5 checksum or SHA checksum for file in PowerShell!

PowerShell Find File by WildCard

To find all the files in the directory or subdirectories that match the PowerShell wildcard.

PS D:\Temp> Get-ChildItem | Where-Object { $_.Name -match '[a-z].txt$' }


    Directory: D:\Temp


Mode                LastWriteTime         Length Name                                                        
----                -------------         ------ ----                                                        
-a----       10-07-2021     11:11            196 Replace-Method.txt  

In the above example, it finds files using PowerShell wildcard as [a-z].txt$ and gets a list of all files.

Cool Tip: ErrorAction and ErrorVariable parameters in PowerShell!

PowerShell Find Files Older than Specific Days

To find files older than specific days, use PowerShell Get-ChildItem to get file objects and compare the file creation date with the current date – 15 days.

 Get-ChildItem -File | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-15)} | Select Name, CreationTime | sort CreationTime -Descending

In the above PowerShell script, the first command gets the file object and passes the output to the second command.

Second command, Where-Object Compare the file creation date with the current date – 15 days using the Get-Date cmdlet and pass the output to the third command.

The third command selects the file name and file creation date time format and passes the output to the fourth command.

In the last command, sort file creation date-time descending and get a list of files older than 15 days before.

Conclusion

I hope you find the above article on using the PowerShell Get-ChildItem cmdlet to find files that match search patterns or find files by extension, or name helpful.

I tried to explain different ways to search for files by wildcard, file by name, file by location or directory, or get folders only in the file system.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.