.\MyScript.ps1 2>&1 | tee -filePath c:\results.txt
tee (tee-object) cmdlet redirects output into two directions, stores in a file or var and sends down the pipeline. In our use, it sends to a file and also displays on console.
2>&1 construct, (stderr) 2> redirects the standard and appends the &1 (stderr to stdout) to a filename of results.txt.
stdin – Standard input = 0
stdout – Standard output = 1
stderr – Standard error = 2
> – Redirect pipeline output to a file,overwriting the current contents.
>> – Redirect pipeline output to a file,appending to the existing content.
2>> – Redirect error output to a file,appending to the current contents.
2> – Redirect error output to a file,overwriting the current contents.
2>&1 – The error messages are writtento the output pipe instead of theerror pipe.
Supports PS 1.0,2.0
Supports PS 3.0+; captures only standard output