Powershell search matching string in word document

Question:

I have a simple requirement. I need to search a string in Word document and as result I need to get matching line / some words around in document.

So far, I could successfully search a string in folder containing Word documents but it returns True / False based on whether it could find search string or not.

Answer:


There are a couple of ways to get what you want. A simple approach is since you have the text of the document already lets perform a regex match on it and return the results and more. This helps in trying to address getting some words around in document.

We have the variable $charactersAround which sets the number of characters to match around the $findtext. Also I though the output was a better fit for a CSV file so I used $results to capture a hashtable of properties that, in the end, are output to a csv file.

Be sure to change the variables for your own testing. Now that we are using regex to locate the matches this opens up a world of possibilities.

Sample Output

Source:

Powershell search matching string in word document by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply