# Find a word and add text after that word is found

• ### 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.

Gaz

$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) { "found" "Delete" } Set-Content } } 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. Example: 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 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!
found
delete

---- or -----

There's a Bumblebee on this line!
found
Delete
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,