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

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

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.

Very often, we store files anywhere on the drive where they shouldn’t be put, and later after a few months, we have so many files copied at different locations and remain an unorganized way.

If we try to find a file stored anywhere on the drive, using a manual search or windows provided search filter with wild card characters takes a lot of time and more frustration.

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 Get-ChildItem

Using 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 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

Above command, find files in the current directory that matches extension .txt

PowerShell Find all files on the root of drive D:\

To find and list all files stored on drive D:\ location, using Get-ChildItem is given below

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

Above Get-ChildItem cmdlet takes D:\ as path and lists all the directory 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, using PowerShell Get-ChildItem with Recurse parameter in PowerShell.

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

Using recurse parameter 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 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 newline to string or variable?

Search for files that do not match using exclude parameter

To find all files in current and subdirectory that do not match PowerShell wildcard *.exe, we can do it using exclude parameter

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

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

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

Get a list of all files in directory that matches a pattern

To get a list of all files in directory and subdirectory that matches PowerShell wildcard pattern *.doc,*.docx

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 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

Using -Filter parameter to get list of all files from subdirectories that match filter *.txt

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

Above Get-ChildItem command using Recurse parameter to recursively iterate in folder and subfolders and filter parameter with *.txt to get only *.txt file extension files only.

Above PowerShell script find files recursively with extension.

Cool Tip: Replace text in 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 Recurse parameter to recursively find all files.

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

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

We can also use 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 string

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

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

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

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

We can also use PowerShell Get-ChildItem alias gci to query and list all files within 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 directory or subdirectories that match 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 file creation date with 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 file creation date with current date – 15 days using Get-Date cmdlet and passes the output to the third command.

The third command selects 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 gets 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.

Leave a Comment