11# ###############################################################################
22<#
33. SYNOPSIS
4- Searches for file- or directory- names with optionally filtering regex content matching
4+ Searches for files or directories with optional content filtering.
55
66. DESCRIPTION
7- Searches for file- or directory- names, optionally performs a regular expression
8- match within the content of each matched file.
7+ Performs recursive file and directory searches with support for:
8+ - File/directory name patterns
9+ - Content matching using regular expressions
10+ - Searching across all drives
11+ - Directory-only searches
12+ - Object pipeline output
913
1014. PARAMETER SearchMask
11- Specify the file name or pattern to search for. Default is "*".
15+ The file/directory name pattern to search for. Supports wildcards.
16+ Defaults to "*" if not specified.
1217
1318. PARAMETER Pattern
14- Regular expression pattern to search within the content of files to match against. Default is ".*".
19+ Regular expression to match against file contents.
20+ Only applies when searching files, not directories.
1521
1622. PARAMETER AllDrives
17- Search all drives.
23+ Search across all available drives instead of just the current path .
1824
1925. PARAMETER Directory
20- Search for directories only.
26+ Search for directories only, ignoring files .
2127
2228. PARAMETER PassThru
23- Pass through the objects to the pipeline .
29+ Output matched items as objects instead of formatted strings .
2430
2531. EXAMPLE
26- # Find all files with the .txt extension in the current directory and its subdirectories
32+ # Search for all .txt files in current directory and subdirectories
2733Find-Item -SearchMask "*.txt"
2834
29- # or in short
30- l *.txt
31-
3235. EXAMPLE
3336# Find all files with that have the word "translation" in their name
3437Find-Item -SearchMask "*translation*"
@@ -113,7 +116,7 @@ function Find-Item {
113116 Mandatory = $false ,
114117 Position = 1 ,
115118 ParameterSetName = ' WithPattern' ,
116- HelpMessage = " Regular expression pattern to search within the content of files to match against "
119+ HelpMessage = " Regular expression pattern to search within file contents "
117120 )]
118121 [Alias (" mc" , " matchcontent" )]
119122 [PSDefaultValue (Value = " .*" )]
@@ -144,23 +147,36 @@ function Find-Item {
144147
145148 begin {
146149
150+ Write-Verbose " Starting Find-Item with SearchMask: $SearchMask "
151+
152+ # normalize and validate the search mask
147153 $SearchMask = $SearchMask.Trim ()
148154 if ($SearchMask -eq [string ]::Empty) {
149-
150155 $SearchMask = " .\*"
151156 }
152- $SearchMask = $SearchMask.Trim ().Replace(" \" , [IO.Path ]::DirectorySeparatorChar).Replace(" /" , [IO.Path ]::DirectorySeparatorChar).Replace([IO.Path ]::DirectorySeparatorChar + [IO.Path ]::DirectorySeparatorChar, [IO.Path ]::DirectorySeparatorChar)
153157
154- if ($SearchMask.EndsWith ([IO.Path ]::DirectorySeparatorChar)) {
158+ # normalize path separators
159+ $SearchMask = $SearchMask.Trim ().Replace(" \" , [IO.Path ]::DirectorySeparatorChar).`
160+ Replace(" /" , [IO.Path ]::DirectorySeparatorChar).`
161+ Replace([IO.Path ]::DirectorySeparatorChar + [IO.Path ]::DirectorySeparatorChar,
162+ [IO.Path ]::DirectorySeparatorChar)
155163
164+ # ensure directory paths end with wildcard
165+ if ($SearchMask.EndsWith ([IO.Path ]::DirectorySeparatorChar)) {
156166 $SearchMask += " *"
157167 }
158168
159- # expand search mask path to full path
169+ Write-Verbose " Normalized SearchMask: $SearchMask "
170+
171+ # convert to full path
160172 $SearchMask = Expand-Path $SearchMask
173+ Write-Verbose " Expanded SearchMask: $SearchMask "
161174
162- # get current directory for relative path handling
175+ # store current location for relative path handling
163176 $location = Get-Location | ForEach-Object Path
177+ }
178+
179+ process {
164180
165181 # helper function to search file content
166182 function Search-FileContent {
@@ -172,11 +188,6 @@ function Find-Item {
172188 return Select-String - Path $FilePath - Pattern $Pattern
173189 }
174190
175- $SearchMask = Expand-Path $SearchMask
176- }
177-
178- process {
179-
180191 # output blank line unless passing through objects
181192 if (-not $PassThru ) {
182193 " " | Out-Host
@@ -186,14 +197,17 @@ function Find-Item {
186197 if ((-not $Directory ) -and ($Pattern -ne " .*" ) -and
187198 (-not [string ]::IsNullOrWhiteSpace($Pattern ))) {
188199
189- # searching all drives
200+ Write-Verbose " Searching files with content pattern: $Pattern "
201+
202+ # search all drives
190203 if ($AllDrives ) {
204+ Write-Verbose " Searching across all drives"
191205
192- # get all drives and process in parallel
206+ # process drives in parallel
193207 Get-PSDrive - ErrorAction SilentlyContinue |
194208 ForEach-Object - ThrottleLimit 8 - Parallel {
195209
196- # extract filename from search mask
210+ # extract filename part
197211 $file = [IO.Path ]::GetFileName($SearchMask )
198212 $filter = [string ]::IsNullOrEmpty($file ) ? " *" : $file
199213
@@ -208,7 +222,7 @@ function Find-Item {
208222 Where-Object - Property Name -Like $filter |
209223 ForEach-Object {
210224
211- # check file content for pattern
225+ # check file content
212226 if (Search-FileContent - FilePath $PSItem.FullName `
213227 - Pattern $Pattern ) {
214228
@@ -233,8 +247,9 @@ function Find-Item {
233247 return
234248 }
235249
236- # regular content search in current location
237- Get-ChildItem $SearchMask - File - Recurse | ForEach-Object {
250+ # regular content search
251+ Get-ChildItem $SearchMask - File - Recurse |
252+ ForEach-Object {
238253 if (($Pattern -eq " .*" ) -or
239254 (Search-FileContent - FilePath $PSItem.FullName - Pattern $Pattern )) {
240255
@@ -255,8 +270,10 @@ function Find-Item {
255270 return
256271 }
257272
258- # searching all drives without content pattern
273+ # search all drives without content
259274 if ($AllDrives ) {
275+ Write-Verbose " Searching all drives for matching items"
276+
260277 Get-PSDrive - ErrorAction SilentlyContinue |
261278 ForEach-Object - ThrottleLimit 8 - Parallel {
262279 try {
@@ -282,7 +299,9 @@ function Find-Item {
282299 return
283300 }
284301
285- # regular file/directory search without content pattern
302+ # regular file/directory search
303+ Write-Verbose " Performing regular file/directory search"
304+
286305 $dir = [IO.Path ]::GetDirectoryName($SearchMask )
287306 if ([string ]::IsNullOrEmpty($dir )) {
288307 $dir = " .$ ( [IO.Path ]::DirectorySeparatorChar) "
@@ -321,3 +340,4 @@ function Find-Item {
321340 end {
322341 }
323342}
343+ # ###############################################################################
0 commit comments