title: How to continuously read Serial COM port in Powershell and occasionally write to COM port
body: <p>I need to know how I can continuously read data in from the COM port and dump it to a file using Windows Powershell. While I am reading data in, I also need to monitor the data being read in and, depending on what the last line that was read, write data to the COM port. </p>
  
  <p>To open the COM port in Powershell, I am doing this:</p>
  
  <pre><code>[System.IO.Ports.SerialPort]::getportnames()
  $port= new-Object System.IO.Ports.SerialPort COM3,115200,None,8,one
  $port.open()
  </code></pre>
  
  <p>To read data in to the COM port, I am doing this:</p>
  
  <pre><code>$line=$port.ReadLine()
  </code></pre>
  
  <p>My initial thought was to have the main Powershell script open the COM port. Then it would start a background/child task to continuously read data in from COM port and dump it to a file. While that child task is running, the parent would then continuously monitor the file and write to the COM port when needed.</p>
  
  <p>When I tried to do that, the child could not read data in from the COM port because the parent had it open and the child did not inherit that permission from the parent. </p>
  
  <p>Any ideas on how I can accomplish this?</p>