title: Splitting, rearranging, excluding strings from a text file in Powershell to output to another text file
body: <p>I am outputting a stale tag report from the OSIsoft PI System using piconfig, with the name, time, and value of the tag. I cannot do any sort of editing to how this is presented from within the piconfig script, so I am instead trying to parse the text file (daily-stale-tags-report.txt) to a new text file, which will be automatically emailed out each morning. The text file currently looks something like this:</p>
  
  <pre><code>L01_B000_BuildingName0_Citect_more_tag_info_here,22-Feb-17 14:56:23.55301,3.808521E+07
  L01_B111_BuildingName1_MainElectric_111A01ME_ALC,23-Apr-15 08:45:00,64075.
  L01_B111_BuildingName1_MainSteam_111TC1MS_His_Mozart,20-Jan-17 22:21:34,88778.
  L01_B333_BuildingName3_MainWater_333E02MW_Manual,1-Dec-16 18:00:00,4.380384E+07
  L01_B333_BuildingName3_SubElectric_333B03SE_Mozart,2-Dec-16 18:45:00,70371.
  L01_B333_BuildingName3_Citect_more_tag_333_info_here,4-Jan-17 10:08:33.24501,111730
  </code></pre>
  
  <p>I need to exclude any tag ending in '_Manual' or that contains '<em>_His_</em>', with the goal of outputting a text file that appears more like this:</p>
  
  <blockquote>
    <p>B000 BuildingName0</p>
    
    <p>L01_B000_BuildingName0_Citect_more_tag_info_here,22-Feb-17
    14:56,3.808521E+07 </p>
    
    <p>B111 BuildingName1</p>
    
    <p>L01_B111_BuildingName1_MainElectric_111A01ME_ALC,23-Apr-15
    08:45,64075.</p>
    
    <p>B333 BuildingName3</p>
    
    <p>L01_B333_BuildingName3_SubElectric_333B03SE_Mozart,2-Dec-16
    18:45,70371.
    L01_B333_BuildingName3_Citect_more_tag_333_info_here,4-Jan-17
    10:08,111730.</p>
  </blockquote>
  
  <p>I am basically a newbie at all this (yesterday's activity of generating and successfully emailing the report was a major feat for me), so I am trying to work off of basic articles and questions people have previously asked. I managed to add headers using this article: <a href="https://www.petri.com/powershell-import-csv-cmdlet-parse-comma-delimited-csv-text-file" rel="nofollow noreferrer">https://www.petri.com/powershell-import-csv-cmdlet-parse-comma-delimited-csv-text-file</a> which I assume would look something like this:</p>
  
  <pre><code>$input = import-csv "c:daily-stale-tags-report.txt" -header Tag,Date,Value
  </code></pre>
  
  <p>I then moved on to this article <a href="https://www.petri.com/powershell-string-parsing-with-substring" rel="nofollow noreferrer">https://www.petri.com/powershell-string-parsing-with-substring</a> to try and split up my data using the underscore as the delimiter, with the goal of extracting the Building Code (e.g. B000) and BuildingName. </p>
  
  <pre><code>ForEach ($tag in $input) {$t = ($s -split '_',4)[1..2]}
  </code></pre>
  
  <p>Lastly, I was trying to use this article <a href="https://stackoverflow.com/questions/29999682/powershell-parsing-a-text-file">powershell Parsing a text file</a> but I'm stuck as this example doesn't quite apply.</p>
  
  <p>From what I've read, Get-Content wouldn't really work here because I have more than one piece of information on a line. If anyone could point me in a good direction to head (or whether this can even be done the way I have my example above), I'd greatly appreciate it. </p>