Powershell: Search docx and doc content in folders

I’ve recently needed to search the content of a huge set of shared folders for documents containing a specific word.

Powershell made it fairly easy. I’ve also included PDF into the mix as it _sometimes_ works for the older format PDF documents.

$directoryToSearch = 'C:\temp'
$lookingfor = 'surreysendlo'
$word = New-Object -ComObject Word.Application
Get-ChildItem -Path $directoryToSearch -Include "*.doc*", "*.pdf" -Recurse | foreach-object {
  $file = $_.FullName
  if ($_.FullName.SubString(5) -match '.docx') {
    if ($word.Documents.Open($file).Content.Find.Execute($lookingfor)) {
      write-host WARNING: $_.FullName contains $lookingfor
  } else {
    if ((Get-Content $file | %{$_ -match $lookingfor }) -contains $true) {
        write-host WARNING: $_.FullName contains $lookingfor
        #Add-Content c:\temp\log.txt WARNING: $_.FullName contains $lookingfor
This entry was posted in Uncategorized. Bookmark the permalink.

Comment on this topic

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s