0

When I search the word "computer" on my folder that contains several .docx files, windows list the list of files which contains this word, which is good.

But I would like to see more than the list of file names. I want to see a preview of the file where the word has been found (it could simply show X number of character/word before and after the keyword, or the full sentence).

(I'm not asking about app which could do that. I would like to use the windows search feature)

Is it possible?

Any solution would fit: using win file explorer, the cmd console or PowerShell.

1 Answer 1

0

So, you posted this on a PowerShell script assistant forum and this does not appear to be a PS question / issue but a general Windows Explorer use case question.

If you are saying you are doing this in PS, then you have to write that yourself.

Or, just use windows explorer, after setting advanced preference to search file content, then do a search and leverage preview mode, using the view menu, or pin that item to the explorer quick launch bar for easy access. This will give you a resizable split pane to the right of your file explorer list. Click a file name and it will show you the entire file contents, depending on how the file formatted.

Demo'd here:

https://www.youtube.com/watch?v=PSSDzrra21g

Update based on OP response

Alight, but what you are asking about is not something out of the box. You have to write this yourself as I stated earlier.

Windows explorer can search virtually any file content, as long as you set it to, but it will not show you a preview without preview mode, and preview is just a tool to open the file, using the files default app.

You are asking to do a search, and land on that spot in a file based on a keyword. Again, not something out of the box on Window in any way.

So, it's a write from scratch effort. We'll help with code you've written, but not write it from scratch for you. Well, sometimes it happens, if someone is feeling really generous. However, that is what consultants are for.

Your code must find the file, keyword, open the file using the file app engine, then you have to navigate via that file object model to use the keyword to search it once opened and land on the keyword location.

This is not a off the cuff thing. It requires knowledge of each step intimately, and calling into the parent app to show it. So, it's not an off the cuff thing.

There are samples around the web on how to do this for different file types. Example:

https://social.technet.microsoft.com/wiki/contents/articles/29851.powershell-trick-search-highlight-text-in-ms-word.aspx

$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open("C:\temp\test.docx")


$FindText = "document"

  foreach ($docrange in $objDoc.Words)
  {
     if ($docrange.Text.Trim() -eq $FindText)
     {
        $docrange.highlightColorIndex = [Microsoft.Office.Interop.Word.WdColorIndex]::wdYellow 
     }
  }

Now, that is just for Word. If you have Excel, PowerPoint, Visio, PDF (this requires an addon), text (this is easiest), etc., they each have their own object model you have to know and use.

1
  • thanks for your reply, but the preview pane is useless in that case since it doesn't highlight the keyword that has been found (and we can't search within this pane). Concerning the PowerShell console, I would use it if I had the right code (that's was I asked this question)
    – JinSnow
    Commented Oct 7, 2018 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .