title: how to output a string to variable and console at the same time
body: <p>is there an easy way in powershell to output a string to variable and console at the same time?</p>
  
  <p>i want to capture the output of my script to a variable so i can analyze it in the end of the script, save it to a log file and also email to an operator.</p>
  
  <p>my intent is to have a variable $output and add any output strings to it and also output to console immediately something like</p>
  
  <pre><code>$output="Process started"
  
  $output=+"Doing step 1"
  "Doing step 1"
  
  $output=+"Doing step 2"
  "Doing step 2"
  </code></pre>
  
  <p>so in the end I can save $output to a log file, email it and parse it.</p>
  
  <p>I played with tee-object that might work for that purpose but unfortunately it would rewrite my $output variable instead of appending a string to it. </p>
  
  <p><strong>UPDATE</strong>
  This is the final solution I decided to go with - thanks to manojlds!</p>
  
  <pre><code>$script:output = ""
  
  filter mylog {
      $script:output+= $_+"`n"
      return $_
  }
  
  
  "doing step {0}" -f 1 | mylog
  "doing step {0}" -f 2 | mylog
  "doing step {0}" -f 3 | mylog
  
  #in the end of the script
  $script:output
  </code></pre>