Hi,
I am trying to write a powershell script to search and find if the files exist in a given directory or Drive. and then find the fullpath of the file location.
In the Filestoscan.txt I have some 700 file names (only the file names, without the full path) which can be present anywhere in the C:\Program Files (x86).
I am doing a search in C:\Program Files (x86) to find these file names. The search is taking very long time to complete. I am doing a foreach loop on every filename and executing get-childitem on the whole C:\Program Files (x86). .
then iterate through next file...... search the whole C:\Program Files (x86).
It takes about 30 minutes to finish the search. I am afraid, If i need to search one level up, C:\ or search for more files, it is going to take infinite amount of time.
Is it the foreach loop i am not using properly causing the slowness or does the powershell takes this long for search normally.
Please advice if there is any better/efficient method to search the files.
CODE:
$var=(Get-Content $DirPath\Filestoscan.txt)
foreach ($filename in $var) {
[string]$filename=$filename.Trim();
Get-ChildItem -Recurse -Force "C:\Program Files (x86)" -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*$filename*") } | Select-Object FullName,Directory >> "$DirPath\fileResultOutput.txt"
}