title: Replace string in text file using PowerShell
body: <p>I have a text file at some location on the server. This file contains a string value and a timestamp.
  I want to replace this specific line with the latest timestamp using PowerShell.</p>
  
  <p>Below is the notepad file's data:</p>
  
  <pre>
  Test Service|05-09-2017 01-09-41
  </pre>
  
  <p>Here 05-09-2017 01-09-41 is MM-dd-yyyy HH-mm-ss and I want to replace this line with the latest timestamp.</p>
  
  <p>So, if after two minutes it should be </p>
  
  <pre>
  Test Service|05-09-2017 01-11-41
  </pre>
  
  <p>Here is what I am doing:</p>
  
  <pre><code>$ReplaceString = "$ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')"
  Get-Content Path -Replace($ServiceName + "|" + $StringLastRunTime,$ReplaceString) | Set-Content Path
  </code></pre>
  
  <p>But it does not generate expected output.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Sorry for not being so clear with requirements</p>
  
  <p>I want to monitor critical services installed on the server using PowerShell script. I have listed all the services to be monitored in one text file and that path has been mentioned in the script. This script will execute every 5 minutes and if service is not installed or service is stopped it will generate an email notification. To prevent email flood I am thinking of updating a timestamp with the service name in the text file.
  So, if I find one service in stopped mode or not installed, I will send a notification and will update the timestamp of when last it was sent. I have a resend interval which will check every five minutes and if that interval is passed, I have to again send the notification and I want to update the timestamp.</p>
  
  <p>Code (<em>I have removed email sending portion to make it less complex</em>):</p>
  
  <pre><code>$ServicesList = Get-Content E:\ServiceReports\Services.txt
  $ResendEmailInterval = 10 #In Minutes
  
  foreach ($Service in $ServicesList) {
      $ServiceName = $Service
      $Service = Get-Service -Display $Service -ErrorAction SilentlyContinue
      $serviceDisplayName = $Service.displayname
      if (-Not $Service) {
          #Service is not installed. So need to send an email alert and update the timestamp.
          Write-Host $ServiceName.IndexOf("|")
          if ($ServiceName.IndexOf("|") -gt 0) {
              $ServiceName,$StringLastRunTime = $ServiceName.Split('|')
              $LastRunTime = [datetime]::ParseExact($StringLastRunTime, "MM-dd-yyyy HH-mm-ss", $null)
              Write-Host $ServiceName
              Write-Host $LastRunTime
              Write-Host (Get-Date).AddMinutes($ResendEmailInterval)
              if ($LastRunTime -lt (Get-Date).AddMinutes($ResendEmailInterval)) {
                  Write-Host "time to run"
                  $ReplaceString = $ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')
                  Write-Host $ServiceName"|"$StringLastRunTime
                  Write-Host $ReplaceString
                  #$ServiceName = "Test Service";
                  $ReplaceString = "{0}|{1}" -f $ServiceName, (Get-Date).ToString('MM-dd-yyyy HH-mm-ss');
                  (Get-Content -Path $ServicesListPath) -replace ('([^|]+).*', $ReplaceString) | Set-Content $ServicesListPath;
                  $ServicesList -replace ($ServiceName + "|" + $StringLastRunTime, $ReplaceString) | Set-Content $ServicesListPath
              }
          } else {
              $ReplaceString = $ServiceName + "|" + (Get-Date).ToString('MM-dd-yyyy HH-mm-ss')
              $ServicesList -replace ($ServiceName, $ReplaceString) | Set-Content $ServicesListPath
              $i = $i+1;
          }
      } else {
          if ($Service.Status -eq "Stopped") {
              Write-Host "Service is stopped"
              #Time to send email and update the time stamp
              $i = $i+1;
          } else {
              #Write-Host "Service is running. Nothing to do."
          }
      }
  }
  </code></pre>