Console output copypaste

How to output console or PowerShell transcript to a file in Windows

This post was most recently updated on July 13th, 2022.

4 min read.

Every now and then, you will run into a situation, where you will need to somehow dump the console output (or transcript) of running a console application. I’m actually going to argue it happens a lot more often than one would think – in my case, any time a customer requires a webjob or a function, that one would normally deploy to Azure, being run on the servers of the customer. You’ll need a way to store console (or PowerShell) output into a file.

This post describes how to do that.

Problem

Something breaks or the app crashes, and the error is logged to the event log… But just the error, not the whole transcript. You’d like to get it all, to figure out what’s actually going on, but an event log is not the way to go.

Or, maybe you’re investigating an error that happened to someone else, but only get screenshots of console or event log errors, whereas you’d want to get all the possible information about the problem instead.

What to do?

Solution: redirection operator > to the rescue!

It’s luckily pretty easy. There are multiple ways to pipe, dump, redirect, log, mirror or just save the output to almost any imaginable target medium, but since I hate always googling for them (and for the life of me, I can’t seem to remember it by heart), I’m documenting my preferred way here.

No need to pipe the output to a file – because the redirection does natively what you could do with a pipe and “Out-File -FilePath” or similar command.

Time needed: 2 minutes

How to redirect console or PowerShell output to a file in Windows?

  1. Redirect output to file in Powershell

    You can direct the whole console output (and hence the whole PowerShell transcript for your executable) to a text file by doing something like this:

    executable.exe > output.txt 2>&1

    OR

    executable.exe *>&1 > output.txt

    This method just writes everything from the console window to a file – as simple as that!

    How to redirect your custom executable's console output to a file
    How to redirect your custom executable’s console output to a file


    And of course, it’s not only limited to Command Prompt (cmd.exe) – it also works in Windows PowerShell:
    How to redirect your custom executable's PowerShell console output to a file
    How to redirect your custom executable’s PowerShell console output to a file

  2. Verify that it has worked (optional)

    If you want to verify this works, you can run the following in PowerShell – it first outputs “Hello World” and then simply redirects it to the file output.txt:

    echo "Hello World" >output.txt 2>&1
    .\output.txt


    The second command should open output.txt in the text editor – and it should contain “Hello World”.

  3. And that’s it!

    Yep – it’s that simple! But read on for more details.


In these examples, the following parameters affect the outcome:

ElementDescription
Redirection operator: >Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window.
2>&1These parameters cause this command to first redirect stdout (Standard Output Stream) to the output file and then redirect stderr (Standard Error Stream) there as well.
For a more thorough list of different operators available, see here.

Okay – so now it’s finally documented. Maybe I won’t have to google it the next time :)

Wait… But how is this better than copypasting from a console window?

If you’re not running your application unattended, you could just run the application and copy-paste from the window to your preferred location. That’s nice and easy! So what makes this method better?

A couple of things come to mind:

Why dumping console output to a file is better than just copypasting

  1. You can get the transcripts unattended. You don’t have to do anything yourself.
  2. This way, You won’t mess up the copying by selecting from areas of the output.
    • I don’t know about you, but I often mess up the copy-pasting from a console or PowerShell window. This method makes that impossible.
  3. This is the easiest way I’ve found to ask other people for the whole output/transcript of a console application run.
    • That’s really useful because when I’m debugging, I really want the whole log and not just the last few lines of red text!

Further thoughts

For more info and options about the output scenarios, see this Stack Overflow thread. If you want to read more about stdout and stderr, check this out.

And for further reference, here’s a list of all supported redirection operators for reasonably recent PowerShell versions (probably valid for PowerShell >5.1):

RedirectionWhat’s it do?
>Sends output to the specified file
>>Appends output to the content of the
specified file
2>Sends errors to the specified file.
2>>Appends errors to the content of the
specified file
2>&1Sends errors and success output to the
success output stream
3>Sends warnings to the specified file
3>>Appends warnings to the contents of the
specified file.
3>&1Sends warnings and success output to the
success output stream
4>Sends verbose output to the specified file
4>>Appends verbose output to the contents of
the specified file
4>&1Sends verbose output and success output to
the success output stream
5>Sends debug messages to the specified file
5>>Appends debug messages to the contents of
the specified file
5>&1Sends debug messages and success output to
the success output stream
6>Sends informational messages to a specified
file
6>>Appends informational messages to the
contents of a specified file
6>&1Sends informational messages and success
output to the success output stream.
*>Sends all output types to the specified file
*>>Appends all output types to the contents of
the specified file
*>&1Sends all output types to the successful output
stream
Source (applied)

However, a far more advanced scenario would be to save the output directly to Application Insights or something similar. For a lot of cases, this would be like shooting a fly with a bazooka, but for larger deployments, why not.

Maybe worth a blog post later!

mm
5 2 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
most voted
newest oldest
Inline Feedbacks
View all comments