Home > Enterprise >  PowerShell - Return file search results with the ability to select which file you want to open
PowerShell - Return file search results with the ability to select which file you want to open

Time:12-26

Here is my code which works but I'm not sure how to add the ability to number the results and select which file to open.

I'm not a programmer by no means it took me a week just to get this far so any help would be greatly appreciated. Thank you

# Find pdf files by name on my network share then select one from the list of return matches to open 

Write-Host "Enter a File Name" 

$fileName = Read-Host

# Set the path to the folder you want to search
$folderPath = "\\MyNetworkShare"

$pdfFile = Get-ChildItem -path $folderPath  -Recurse -Filter *.pdf |Where-Object {$_.Name -match $fileName} | Select-Object -ExpandProperty FullName

# If there is a match, open the PDF in Microsoft Edge
if ($pdfFile)
{
    Start-Process -FilePath "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" $pdfFile
    Write-Host "$pdfFile"
    pause 10
}
else {
    Write-Host "File not found"
}

Current Output

Enter a File Name
465430
\\MyNetworkShare\Tk.465430.1668780736.001.pdf
Press Enter to continue...: 
PS C:\Users\timothy\Desktop\POWERSHELL>

I would like the output to be:

No File
-- ----
1  Tk.465430.1668780736.001.pdf
2  Tk.465430.1668780737.001.pdf
3  Tk.465430.1668780738.001.pdf

Select a file to open:

CodePudding user response:

You can use a for loop to iterate through the files returned while assigning them their index number in the list of $pdfFile. Since im very fond of PSCustomObjects I will use that for the demonstration's purposes, but this can be done other ways with the simplest probably being piping to Out-GridView:

$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."
}

The code needed to run this should just be a for loop with a selection option, but due to how Read-Host beats the object output to the pipeline, there's some workarounds implemented. Also, starting the selection at 1 causes you to subtract by 1 on the selected since arrays start at 0.

  • Related