title: Powershell script to search through a directory of excel files to find a string only searching through 1 file
body: <p>I have found this script on <a href="https://shuaiber.medium.com/" rel="nofollow noreferrer">https://shuaiber.medium.com/</a>
  I want to use it to find a certain string in a folder full of excel files.
  the problem I am encountering is that it basically only searches through 1 file and then stops...</p>
  <p>here is the script</p>
  <pre><code>Function Search-Excel {
      [cmdletbinding()]
      Param (
          [parameter(Mandatory, ValueFromPipeline)]
          [ValidateScript({
              Try {
                  If (Test-Path -Path $_) {$True}
                  Else {Throw &quot;$($_) is not a valid path!&quot;}
              }
              Catch {
                  Throw $_
              }
          })]
          [string]$Source,
          [parameter(Mandatory)]
          [string]$SearchText
          #You can specify wildcard characters (*, ?)
      )
      $Excel = New-Object -ComObject Excel.Application
      Try {
          $Source = Convert-Path $Source
      }
      Catch {
          Write-Warning &quot;Unable locate full path of $($Source)&quot;
          BREAK
      }
      $Workbook = $Excel.Workbooks.Open($Source)
      ForEach ($Worksheet in @($Workbook.Sheets)) {
         
          $Found = $WorkSheet.Cells.Find($SearchText) #What
          If ($Found) {
              
              $BeginAddress = $Found.Address(0,0,1,1)
              #Initial Found Cell
              [pscustomobject]@{
                  WorkSheet = $Worksheet.Name
                  Column = $Found.Column
                  Row =$Found.Row
                  Text = $Found.Text
                  Address = $BeginAddress
              }
              Do {
                  $Found = $WorkSheet.Cells.FindNext($Found)
                  $Address = $Found.Address(0,0,1,1)
                  If ($Address -eq $BeginAddress) {
                      BREAK
                  }
                  [pscustomobject]@{
                      WorkSheet = $Worksheet.Name
                      Column = $Found.Column
                      Row =$Found.Row
                      Text = $Found.Text
                      Address = $Address
                  }                 
              } Until ($False)
          }
          Else {
              Write-Warning &quot;[$($WorkSheet.Name)] Nothing Found!&quot;
          }
      }
      $workbook.close($false)
      [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel)
      [gc]::Collect()
      [gc]::WaitForPendingFinalizers()
      Remove-Variable excel -ErrorAction SilentlyContinue
  }
  </code></pre>
  <p>And then I would use</p>
  <p>Get-ChildItem -Path &quot;C:\excelfiles&quot; -Recurse -Include *.xls, *.xlsx, *.xlsm | Select-Object -Property Directory, Name | ForEach-Object { &quot;{0}{1}&quot; -f $<em>.Directory, $</em>.Name } | Search-Excel -SearchText <strong>MyText</strong></p>
  <p>I know its only searching through 1 file because I looked at another file and tried to get it to send me back to confirm yet it doesnt work.</p>
  <p>Any help would be greatly appreciated.</p>