Home > Mobile >  PowerShell - Get-ChildItem too slow while searching for files on a Network Share
PowerShell - Get-ChildItem too slow while searching for files on a Network Share

Time:12-27

Here is my code I have spent all week on, of course with the help of stackoverflow to search for files by name and it works except Get-ChildItem seems to be very slow while searching our network shared folder that has over 150k files.

Is there a better way to write this? Thank you in advanced.

$fileName = Read-Host -Prompt 'Enter a File Name'

# Set the path to the folder you want to search
$folderPath = '\\networkshare'

if ($pdfFile = Get-ChildItem -Path $folderPath -Filter "*$fileName*.pdf" -File -Recurse )
{
    & {
        for ($i = 0; $i -lt $pdfFile.Count; $i  )
        {
            [pscustomobject]@{
                $('#'   (' ' * $pdfFile.Count.Length)) = $i   1
                PDFName = '{0}' -f $pdfFile[$i].BaseName
                Created = '{0}' -f $pdfFile[$i].CreationTime
            }
        }
    } | Out-String -Stream
    $selection = (Read-Host -Prompt 'Enter PDF(s) to open') -split ',' | %'Trim'
    foreach ($selected in $selection)
    {
        $pdf = $pdfFile[$selected - 1]
        Start-Process -FilePath "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" $pdf.FullName
        Write-Host $pdf
        pause 10
    }
}
else
{
    Write-Host -Object "No file with name `"$fileName`" found."
}

My code just asks the user to enter the file they are looking for (which are all PDF files only). Example of what the code does.

Enter a File Name: 1234

Returns files simular to what the user entered

#  PDFName           Created
-- -------           -------
 1 TK1234            12/14/2022 6:19:45 AM 
 2 TK5467-5001234215 12/24/2022 10:57:36 AM

This works great testing this on my c:\temp folder but when I try our network share it takes a long time to get the results.

CodePudding user response:

This can be faster using a enter image description here

It is fast and dirty like a blunt knife, it would be better using an ifilter aware tool like AgentRansack

enter image description here

  • Related