How to find files in directories with PowerShell?
Using PowerShell get-childitem or gci for short searching
for filename with specific pattern is quite an easy task.
In combination with regex search can be accurate or complex.
PowerShell code below will search recursively on the path
specified and will return the file name that matches the pattern.
If the path has multiple subfolders, the subfolder along
with the filename will be shown if it matches the pattern.
Get-ChildItem –Path "C:\Users\" -name -Recurse |
Select-String -Pattern "7"
or
gci -path "C:\Users\" -name -recurse |
Select-String -Pattern "7"
-pattern can be replaced with regex pattern
Sample output:
Win 7 VM.xlsm
New folder\win 7.txt
Win 7 VM.xlsm -no folder on the path which means file is
located under c:\Users\
New folder\win 7.txt - located on a subfolder complete path
is c:\Users\New folder\
Here's another example to search for a specific file name that matches with a specific pattern.
(gci -path "d:\txt notes\" -name -recurse ) -like 'email*' | Select-String -Pattern "7"
Sample Output:
email about 7th grade.txt
email 7 pcs. apple.txt
It will output any file name that starts with email and has the letter 7 as part of the file name.
Cheers.. hope it helps..
Comments
Post a Comment