Find a word and add text after that word is found RRS feed

  • Question

  • Hi Folks,

    I have the following script and I cannot figure out what I am doing wrong. I want to search for the word Bumble in all.eml files in a specific directory. The script should search for the word bumble and if found add "Found" "Delete" below the word Bumble in each file where it is found.

    Guidance is appreciated.

    Thanks in Advance


    $Path = "D:\Temp\text"
    $searchWords = 'Bumble'
    Foreach-Object {
    Foreach ($sw in $searchWords)
        Get-Childitem -Path "$Path" -Recurse -include "*.eml" | 
        Select-String -Pattern "$sw" | 
        Select Path,LineNumber,@{n='SearchWord';e={$sw}}
    IF ($_-match $sw)

    Wednesday, October 9, 2019 7:25 PM

All replies

  • Start by learning how to use the CmdLets by reading the help for each one.  You are using them wrong.

     Get-Childitem -$Path\*.eml -Recurse

    help Get-ChildItem -online
    help Search-String -online

    It is never necessary to place quotes around a variable that is already a string.  Using quotes withut understanding them will lead you into some weird errors.

    help  about_Quoting_Rules
    help about_foreach
    help ForEach-Object -online


    Wednesday, October 9, 2019 8:51 PM
  • In addiion to JRV's comments, you'll need to use "Get-Contents" on each of the files.

    Also, are you trying to place the two words "found" and "Delete" below the word "Bumble" on two additional lines, or just place the words "found" and "Delete" on two lines below the line containing the word "Bumble" regardless of where on the line "Bumble" appears?

    For example:

    There's a Bumblebee on this line!
    ---- or -----
    There's a Bumblebee on this line!
    Also, are you inserting the "found" and "Delete" into the existing file and then rewriting the file? In your example, I have no idea what the Set-Content is supposed to be doing.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, October 9, 2019 9:24 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


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

    Friday, November 8, 2019 2:10 PM