locked
powershell script to find a string in word document and move to other location if exists RRS feed

  • Question

  • Hi i am completely new to Powershell topics...

    I am looking to find a string in word and excel document E.g Like "Security" if it contains in any of the document, the same should be moved to other location...can anyone suggest with simple scripts pls...!!

    This simple script worked in finding and relocating the file, but it is not working for docx format, pls help out..

    Get-ChildItem -Path C:\Users\Administrators\Documents -Include *.txt, *.doc, *.xls -Recurse |Select-String -Pattern "Security" -List|Move-Item -Destination E:\check -Verbose..

    Sunday, October 22, 2017 3:16 PM

Answers

  • Hi,

    I agree with jrv.

    In addition, based on my research, you could have a try with the following scripts for Word documents. Hope it is helpful to you:
    #Stop-Process -Name 'WINWORD'
    $word = New-Object -ComObject Word.Application
    $source = 'D:\source'
    $destination = 'D:\destination'
    $docs = Get-ChildItem -Path $source | Where-Object {$_.Name -match '.docx'}
    foreach ($doc in $docs)
    {
        if ($word.Documents.Open($doc.FullName).Content.Find.Execute('Security'))
        {
            $word.Application.ActiveDocument.Close()
            Write-Host "$doc contains 'Security'"
            Move-Item -Path $doc.FullName -Destination $destination -Verbose
        }
        else
        {
            $word.Application.ActiveDocument.Close()
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 23, 2017 1:44 AM

All replies

  • Select-String will not work on binary files.  You will need to use one of the Excel or MSWord modules or objects to do this:


    \_(ツ)_/

    Sunday, October 22, 2017 5:09 PM
  • Yah, i googled and founded the same answer you mentioned, i tried few options, writting

    $word= New-Object -Comobject word.application

    and conditioning in Foreach If statement, it opens the particular string containing Word document, but not moving the desired option, your help will be more appreciated pls...

    Monday, October 23, 2017 1:00 AM
  • Hi,

    I agree with jrv.

    In addition, based on my research, you could have a try with the following scripts for Word documents. Hope it is helpful to you:
    #Stop-Process -Name 'WINWORD'
    $word = New-Object -ComObject Word.Application
    $source = 'D:\source'
    $destination = 'D:\destination'
    $docs = Get-ChildItem -Path $source | Where-Object {$_.Name -match '.docx'}
    foreach ($doc in $docs)
    {
        if ($word.Documents.Open($doc.FullName).Content.Find.Execute('Security'))
        {
            $word.Application.ActiveDocument.Close()
            Write-Host "$doc contains 'Security'"
            Move-Item -Path $doc.FullName -Destination $destination -Verbose
        }
        else
        {
            $word.Application.ActiveDocument.Close()
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 23, 2017 1:44 AM
  • Word and Excel have "Find" methods.  Research how to use them.

    \_(ツ)_/

    Monday, October 23, 2017 1:44 AM
  • Hi jrv,

    Thanks for reminding me. Find method should be a better way. And I've modified the scripts above.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 23, 2017 2:04 AM
  • @Albert Ling

    Great.!!!!!!!!!!!!!!!!!!!!!!!!!

    Really awesome worked like a charm....Thanks you sooooo much...!!!!!!!!!!!!!

    Monday, October 23, 2017 2:06 AM
  • Yes i felt about, i just started powershell..!! i tried searching for my required option, i didn't found the exact, so created an account to get expertise help...Great i resolved one of my query, about Excel find option i need to search, if can find this too, it will be more beneficial....

    Thanks you all for your valuable reply....!!!

    Monday, October 23, 2017 2:13 AM