title: Need to find and replace one occurrence of a string in a PowerShell script when the script is run multiple times
body: <p>So for this script I'm having an issue if I run the script multiple times it overwrites the second occurrence of &quot;start-sleep -s 10&quot; with the replaced text &quot;start-sleep -s 30&quot; that  I need to remain the same ie. &quot;start-sleep -s 10&quot;. When I tested this it works great when you run it once, but what if the script is ran multiple times? During testing, when the script is ran more than once, the second instance is then changed to &quot;start-sleep -s 30&quot; when I need it to remain &quot;start-sleep -s 10&quot; every time the script is ran as well as not altering the rest of the file. Is there a way in PS to prevent this? Possibly just have the script search lines 0-115 for example? because the second instance of start-sleep -s 10 is located at line 145. What I essentially need is for the script to find the first instance of &quot;start-sleep -s 10&quot; and replace with &quot;start-sleep -s 30&quot;, leave the second instance of &quot;start-sleep -s 10&quot;alone every time I run this script.  I posted an original question for this before and can add it at the end of this post.</p>
  <pre><code>$ScriptPath = &quot;C:\ScriptsFolder\powershell.ps1&quot;
  $newContent = switch -Wildcard -File $ScriptPath {
      # if this line contains `Start-Sleep -s 10`
      '*Start-Sleep -s 10*' {
          # if we previously matched this
          if($skip) {
              # output this line
              $_
              # and skip below logic
              continue
          }
          # if we didn't match this before (if `$skip` doesn't exist)
          # replace from this line `Start-Sleep -s 10` for `Start-Sleep -s 30`
          $_.Replace('Start-Sleep -s 10', 'Start-Sleep -s 30')
          # and set this variable to `$true` for future matches
          $skip = $true
      }
      # if above condition was not met
      Default {
          # output this line as-is, don't change anything
          $_
      }
  }
  $newContent | Set-Content $ScriptPath
  </code></pre>
  <p>Original Question: I need to find and replace one occurrence of a string in a PowerShell script (script is about 250 lines long). There are two occurrences where the string &quot;start-sleep -s 10&quot; appears within the script. I need to edit and only change the first occurrence to &quot;start-sleep -s 30 and keep the second occurrence &quot;start-sleep -s 10&quot;. The issue I'm having is that there are a couple variants of the script I have to edit so my approach was locate the range of lines where the first occurrence is located, make the change, then save the script and keep everything else as is. New to PowerShell so not sure how to go about this. I keep seeing articles online of how to use Get-Content and Set-Content to find and replace text in a file but I need to change the first occurrence of &quot;start-sleep -s 10&quot; and keep the second one as is.</p>