title: Powershell script to edit each text file in a directory and add a CR before a specific string
body: <p>I need to create a script for a user that will find all .txt files in a selected directory, look for a specific string in each of them, add a carriage return at the start of this specific string, and then output the edited .txt files into a directory that is one above the initial selected directory.</p>
  
  <p>Preferably, it should be able to run without admin privileges.</p>
  
  <p>To be clear, they desire an effect that would change the .txt file from this:</p>
  
  <blockquote>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas fringilla neque finibus velit condimentum, sed feugiat diam iaculis. In hac habitasse platea dictumst. <strong>TEXT STRING</strong> Vestibulum fringilla dui nec diam convallis, et cursus elit finibus. Vivamus porttitor est ac erat gravida pellentesque. Cras rhoncus urna a dui hendrerit auctor. Suspendisse potenti <strong>TEXT STRING</strong>. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p>
  </blockquote>
  
  <p>To this:</p>
  
  <blockquote>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas fringilla neque finibus velit condimentum, sed feugiat diam iaculis. In hac habitasse platea dictumst. 
    <br/><strong>TEXT STRING</strong> Vestibulum fringilla dui nec diam convallis, et cursus elit finibus. Vivamus porttitor est ac erat gravida pellentesque. Cras rhoncus urna a dui hendrerit auctor. Suspendisse potenti 
    <br/><strong>TEXT STRING</strong>. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p>
  </blockquote>
  
  <hr>
  
  <p>This is what I have come up with so far, and it stops working at the Get-Content line:</p>
  
  <pre><code>Function Get-Folder()
  {
  Add-Type -AssemblyName System.Windows.Forms
  
  $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
      RootFolder = 'MyComputer'
      ShowNewFolderButton = $true
  }
  
  [void]$FolderBrowser.ShowDialog()
  $FolderBrowser.SelectedPath
  }
  
  $inputFolder=Get-Folder
  
  foreach($file in Get-ChildItem -name $inputFolder\*.txt){
  
  (Get-Content $file ).Replace("text","`r`n text") | Out-File $inputFolder\..\output\$_
  
  }
  
  pause
  </code></pre>
  
  <p>This is the error it throws when this script is run:</p>
  
  <blockquote>
    <p>Get-Content : Cannot find path 'C:\powershell\testing.txt' because it
    does not exist. At C:\powershell\test_script_02.ps1:22 char:2
    + (Get-Content $file ).Replace("text","<code>r</code>n text") | Out-File $inputFol ...
    +  ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\powershell\testing.txt:String) [Get-Content],
    ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand</p>
    
    <p>You cannot call a method on a null-valued expression. At
    C:\powershell\test_script_02.ps1:22 char:1
    + (Get-Content $file ).Replace("text","<code>r</code>n text") | Out-File $inputFol ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull</p>
    
    <p>Get-Content : Cannot find path 'C:\powershell\testing0.txt' because it
    does not exist. At C:\powershell\test_script_02.ps1:22 char:2
    + (Get-Content $file ).Replace("text","<code>r</code>n text") | Out-File $inputFol ...
    +  ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\powershell\testing0.txt:String) [Get-Content],
    ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand</p>
    
    <p>You cannot call a method on a null-valued expression. At
    C:\powershell\test_script_02.ps1:22 char:1
    + (Get-Content $file ).Replace("text","<code>r</code>n text") | Out-File $inputFol ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull</p>
  </blockquote>
  
  <p>It fails to find each text file in the path, which I know to exist. When the Get-Content line is commented out and replaced with a Write-Host command, the script correctly prints every file in the directory. I'm not sure how it seems to be failing to find the correct files. </p>
  
  <p>I know that there must be better ways to do this, I'm very new to Powershell and this is just what I've cobbled together from web searching around. Any help is greatly appreciated.</p>