SASPYTHON
SASPYTHON

Reputation: 1621

How to find a certain phrase of doc/docx files from a folder by using powershell

Good Morning, I am very new to powershell.

I have been googling around how to find a certain phrase in .doc, .docx from some folder, but I can not find the exact solution what I am looking for.

For example, when I executed this code

Get-ChildItem 'C:\Users\koshiasu\Desktop\adhocs' -Filter *.sql |Select-String -Pattern "children"

It shows like this in the bottom of my powershell

result1

Desktop\adhocs\18722 Parents.sql:11:                     AND EXISTS (SELECT c.id_number FROM children c
Desktop\adhocs\18722 Parents.sql:38:                       AND EXISTS (SELECT c.id_number FROM children c
Desktop\adhocs\2969 ADHOC - Parents in Dallas.sql:11:                     AND EXISTS (SELECT c.id_number FROM children c
Desktop\adhocs\2969 ADHOC - Parents in Dallas.sql:92:                     AND EXISTS (SELECT c.id_number FROM children c

I would like to same thing for .doc,.docx

so I changed the code like this

Get-ChildItem 'C:\Users\koshiasu\Desktop\ADHOCS_WORD' -Filter *.doc, *.docx |Select-String -Pattern "Allocations"

but the error is

Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.
At line:2 char:58
+ Get-ChildItem 'C:\Users\koshiasu\Desktop\ADHOCS_WORD' -Filter <<<<  *.doc, *.docx |Select-String -Pattern "Allocations"
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand

how should I change the code to show like result1

Thank you so much

Upvotes: 0

Views: 2646

Answers (2)

Fredrik
Fredrik

Reputation: 1

Here is another solution how to search for a phrase inside .docx files.

$destination = 'c:\temp\'
$docs = Get-ChildItem -Path $source -Recurse | Where-Object {$_.Name -match 
'.docx'}
foreach ($doc in $docs)
{
    if 
($word.Documents.Open($doc.FullName).Content.Find.Execute('wordtosearchfor'))
    {
        Write-Host "$doc contains 'Test'"
        $docs | Out-File C:\temp\result.txt
    }
    else
    {
        $word.Application.ActiveDocument.Close()
    }
}

Upvotes: 0

colsw
colsw

Reputation: 3326

I had code for something basically identical lying around, tidied up slightly so you can get an idea of what you're looking for:

$Path = "C:\Test"
$Find = "Allocations"
$WordExts = '.docx','.doc','.docm'

$Word = New-Object -ComObject Word.Application #create word obj
$Word.Visible = $false #hide the window

$ValidDocs = Get-ChildItem $Path | ? {$_.Extension -in $WordExts} | ForEach { #Foreach doc/docx/docm file in the above folder
    $Doc = $Word.Documents.Open($_.FullName) #Open the document in the word object
    $Content = $Doc.Content #get the 'content' object from the document
    $Content.MoveStart() | Out-Null #ensure we're searching from the beginning of the doc
                              #term,case sensitive,whole word,wildcard,soundslike,synonyms,direction,wrappingmode
    if ($Content.Find.Execute($Find,$false,        $true,     $false,  $false,    $false,  $true,    1)){ #execute a search
        Write-Host "$($_.Name) contains $($findText)" -ForegroundColor Green
        $_.FullName #store this in $ValidDocs
    } else {
        Write-Host "$($_.Name) does not contain $($findText)" -ForegroundColor Red
    }
    $Doc.Close() #close the individual document
    $Doc = $null #null it just in case
}

$Word.Quit() #quit the word process
$Word = $null #null it just in case

return $ValidDocs #return list of docs with the word in them

Upvotes: 1

Related Questions