title: Using Select-String does not work when trying to match with an "if" statement
body: <p>I have tried a number of different things, but just cant seem to get this to work. I am new to <code>Select-String</code>, and it seems pretty complicated.</p>
  <p>I have a text file with a list of error codes that I hope to match with an <code>if ($errorcode -match $retrieveError)</code>, but the <code>$retrieveError</code> that I am using has a <code>Select-String</code> that returns a much larger string than what I am looking for. For example:</p>
  <p>The string I am looking for is: <em>An error occurred stopping transcription</em></p>
  <p>The string the Select-String returns is: <em>&lt;![LOG[Stop-Transcript : An error occurred stopping transcription: The host is not currently transcribing.]LOG]!&gt;&lt;time=&quot;17:02:46.072+300&quot; date=&quot;12-10-2021&quot; component=&quot;RunPowerShellScript&quot; context=&quot;&quot; type=&quot;3&quot; thread=&quot;8436&quot; file=&quot;main.cpp:76&quot;&gt;' at offset 13. [x-y] range in reverse order.</em></p>
  <p>This is my script:</p>
  <pre><code>####This code is used for copying failed OS Upgrade log files to a destination that can be read by Desktop###
  
  #Get Computer names from list
  $computers = Get-Content -Path &quot;\\@path1\Machines with Win10 Errors.txt&quot;
  
  #Get Error codes from list 
  $errorcodes = Get-Content -Path &quot;\\@path2\Win10 Install Error List.txt&quot;
  
  #If error unknown, move to Unknown folder
  #$moveunknown = Move-Item -Path $logfiledest\&quot;$errorcode&quot;\$computer.txt
  
  #Define computers array and execute actions for each
  ForEach($computer in $computers){
  
      #Define Log File Destination
      $logfiledest = &quot;\\@path3\$computer.txt&quot;
      #Copy log file to $logfiledest and rename with computer name
      Copy-Item -Path C:\Windows\ccm\Logs\smsts.log -Destination $logfiledest
  
      #Define error codes array and execute actions for each
      ForEach($errorcode in $errorcodes){
  
          #Read log files in $logfiledest to match defined log strings in $errorcodes txt file
          $retrieveError = Select-String -Path $logfiledest -SimpleMatch $errorcode -Raw
     ## $retrieveError = Select-String -Path \\$computer\C$\Windows\ccm\Logs\smsts.log -Pattern &quot;$errorcode&quot;
  
      if ($errorcode -match $retrieveError) {  #not working
  
          #Rename log files to include error code
          Rename-Item -Path $logfiledest -NewName $computer+&quot;$errorcode&quot;.txt  #working
          #Move renamed log files into matching folders
          Move-Item -Path $logfiledest&quot;+$errorcode.txt&quot;  #working
         
  
      }else {
          
          Write-Host &quot;nada&quot;
      }
  
  }
  }
  </code></pre>