How to redirect the output of a PowerShell to a file during its execution

699,884

Solution 1

Maybe Start-Transcript would work for you. First stop it if it's already running, then start it, and stop it when done.

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# Do some stuff
Stop-Transcript

You can also have this running while working on stuff and have it saving your command line sessions for later reference.

If you want to completely suppress the error when attempting to stop a transcript that is not transcribing, you could do this:

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue" # or "Stop"

Solution 2

Microsoft has announced on Powershell's Connections web site (2012-02-15 at 4:40 PM) that in version 3.0 they have extended the redirection as a solution to this problem.

In PowerShell 3.0, we've extended output redirection to include the following streams: 
 Pipeline (1) 
 Error    (2) 
 Warning  (3) 
 Verbose  (4) 
 Debug    (5)
 All      (*)

We still use the same operators
 >    Redirect to a file and replace contents
 >>   Redirect to a file and append to existing content
 >&1  Merge with pipeline output

See the "about_Redirection" help article for details and examples.

help about_Redirection

Solution 3

Use:

Write "Stuff to write" | Out-File Outputfile.txt -Append

Solution 4

I take it you can modify MyScript.ps1. Then try to change it like so:

$(
    Here is your current script
) *>&1 > output.txt

I just tried this with PowerShell 3. You can use all the redirect options as in Nathan Hartley's answer.

Solution 5

powershell ".\MyScript.ps1" > test.log
Share:
699,884

Related videos on Youtube

Martin
Author by

Martin

Updated on February 25, 2020

Comments

  • Martin
    Martin about 4 years

    I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:

     .\MyScript.ps1 > output.txt
    

    How do I redirect the output of a PowerShell script during its execution?

  • Richard Berg
    Richard Berg almost 15 years
    Note that start-transcript does not record Write-Error, Write-Verbose, or Write-Debug...only standard output.
  • Robert S Ciaccio
    Robert S Ciaccio over 13 years
    @richard: it appears to do so now. Maybe this is a 2.0 addition, not sure if these answers all apply to 1.0.
  • Rob
    Rob almost 13 years
    "output" | Set-Content -PassThru and "output" | Add-Content -PassThru will also work like Tee-Object, with the added benefit that you can set the Encoding.
  • Mel
    Mel over 12 years
    I'm using almost the same code above. my problem is, Stop-Transcript |out-null still sends error output if transcripting is not started. I need to supress the message as it messes out my layout. -erroraction silentlycontinue doesn't help either. any ideas?
  • Colonel Panic
    Colonel Panic almost 12 years
    At last, I found the docs for Start-Transcript. ( frustratingly not labelled with Powershell version number). It says "The transcript includes all command that the user types and all output that appears on the console". However, I tested Start-Transcript in Powershell 2.0, and found @Richard is right, standard error isn't saved to the transcript. This sucks!
  • Rob
    Rob over 10 years
    Not saying I like this solution. In fact, I find it ugly, hard to remember and inflexible. Seems like adding stream capturing parameters to the Out-*, Set-Content and Add-Content Cmdlets would have done the trick in a more Powershelly fashion. While they are at it, they should also add a -PassThru parameter. Which would effectively make the less than useful Tee-Object Cmdlet obsolete.
  • Fer
    Fer over 9 years
    The asker is explicitly explaining that the script call can't be changed.
  • demongolem
    demongolem over 7 years
    This method does not appear to warn you if you actually have permission to write to the location specified.
  • Zoredache
    Zoredache over 7 years
    I am confused, how does this answer the question being asked? "How do I redirect the output of a PowerShell script during its execution'?
  • Rob
    Rob over 7 years
    You are right, @Zoredache, I seem to have overlooked the "cannot change the way this script is called" requirement. When capturing the majority of the output, Start-Transcript is the way to go. Should I remove this answer?
  • Rob
    Rob over 7 years
    For those coming here for general redirection, Powershell has since added -OutVariable -WarningVariable -ErrorVariable , which directly answers my Jan 3 '14 commentary.
  • Zoredache
    Zoredache over 7 years
    No, it is fine to leave it. Given the number of up-votes it is obviously useful. This question almost certainly is getting Google hits from people that are able to change the way the script is being called.
  • Prof Von Lemongargle
    Prof Von Lemongargle over 7 years
    I like this solution. You can use >> output.txt to append. Anyone know if there is a way to have this create ascii instead of unicode?
  • mplwork
    mplwork over 7 years
    You might try something like this: *>&1 | Out-File $log -Encoding ascii -Append -Width 132 but Powershell is really ugly if you need to precisely control the output.
  • gReX
    gReX about 7 years
    Not related to the question, but helpful for me, I was googling to get the right Syntax with Pipe to Out-File.
  • Paul Masri-Stone
    Paul Masri-Stone almost 7 years
    doesn't solve the OP's question because it doesn't work "during execution". But I've given it +1 because it's useful to know how to pipe in Powershell anyway.
  • Rames Palanisamy
    Rames Palanisamy over 6 years
    This was the only of the many options that worked for my script output.
  • user3505901
    user3505901 almost 6 years
    I prefer this as it works within in your script. Perfect for vagrant.
  • Artyom
    Artyom almost 6 years
    Didn't understand this answer. The question was about redirection from the script itself. So it implies that we do in the script smth like this? if ($run_not_from_the_script) { & $myScript *>&1 | tee -File outfile.txt ; return }
  • Rob
    Rob over 5 years
    I try to never mess with the $ErrorActionPreference variable. Too many unintended consequences. As a workaround, you can capture the errant exception and do nothing with it. Which would look like this... try { Stop-Transcript } catch {}
  • stevec
    stevec almost 5 years
    Random question, but if I were to open this txt file while it's being logged to, what happens? I guess it can no longer be logged to and I lose logs until I close the file?
  • AmazingMiki
    AmazingMiki almost 3 years
    this truncates long line outputs and removes information