Redirect all output windows

About Redirection

Short description

Explains how to redirect output from PowerShell to text files.

Long description

By default, PowerShell sends output to the PowerShell host. Usually this is the console application. However, you can redirect the output to a text file and you can redirect error output to the regular output stream.

You can use the following methods to redirect output:

Use the Out-File cmdlet, which sends command output to a text file. Typically, you use the Out-File cmdlet when you need to use its parameters, such as the Encoding , Force , Width , or NoClobber parameters.

Use the Tee-Object cmdlet, which sends command output to a text file and then sends it to the pipeline.

Use the PowerShell redirection operators. Using the redirection operator with a file target is functionally equivalent to piping to Out-File with no extra parameters.

For more information about streams, see about_Output_Streams.

Redirectable output streams

PowerShell supports redirection of the following output streams.

Stream # Description Introduced in Write Cmdlet
1 Success Stream PowerShell 2.0 Write-Output
2 Error Stream PowerShell 2.0 Write-Error
3 Warning Stream PowerShell 3.0 Write-Warning
4 Verbose Stream PowerShell 3.0 Write-Verbose
5 Debug Stream PowerShell 3.0 Write-Debug
6 Information Stream PowerShell 5.0 Write-Information
* All Streams PowerShell 3.0

There is also a Progress stream in PowerShell, but it does not support redirection.

The Success and Error streams are similar to the stdin and stderr streams of other shells. However, stdin is not connected to the PowerShell pipeline for input.

PowerShell redirection operators

The PowerShell redirection operators are as follows, where n represents the stream number. The Success stream ( 1 ) is the default if no stream is specified.

Operator Description Syntax
> Send specified stream to a file. n>
>> Append specified stream to a file. n>>
>&1 Redirects the specified stream to the Success stream. n>&1

Unlike some Unix shells, you can only redirect other streams to the Success stream.

Examples

Example 1: Redirect errors and output to a file

This example runs dir on one item that will succeed, and one that will error.

It uses 2>&1 to redirect the Error stream to the Success stream, and > to send the resultant Success stream to a file called dir.log

Example 2: Send all Success stream data to a file

This example sends all Success stream data to a file called script.log .

Example 3: Send Success, Warning, and Error streams to a file

This example shows how you can combine redirection operators to achieve a desired result.

  • 3>&1 redirects the Warning stream to the Success stream.
  • 2>&1 redirects the Error stream to the Success stream (which also now includes all Warning stream data)
  • > redirects the Success stream (which now contains both Warning and Error streams) to a file called C:\temp\redirection.log )

Example 4: Redirect all streams to a file

This example sends all streams output from a script called script.ps1 to a file called script.log

Example 5: Suppress all Write-Host and Information stream data

This example suppresses all information stream data. To read more about Information stream cmdlets, see Write-Host and Write-Information

Example 6: Show the effect of Action Preferences

Action Preference variables and parameters can change what gets written to a particular stream. The script in this example shows how the value of $ErrorActionPreference affects what gets written to the Error stream.

When we run this script we get prompted when $ErrorActionPreference is set to Inquire .

When we examine the log file we see the following:

Notes

The redirection operators that do not append data ( > and n> ) overwrite the current contents of the specified file without warning.

However, if the file is a read-only, hidden, or system file, the redirection fails. The append redirection operators ( >> and n>> ) do not write to a read-only file, but they append content to a system or hidden file.

To force the redirection of content to a read-only, hidden, or system file, use the Out-File cmdlet with its Force parameter.

When you are writing to files, the redirection operators use UTF8NoBOM encoding. If the file has a different encoding, the output might not be formatted correctly. To write to files with a different encoding, use the Out-File cmdlet with its Encoding parameter.

Potential confusion with comparison operators

The > operator is not to be confused with the Greater-than comparison operator (often denoted as > in other programming languages).

Depending on the objects being compared, the output using > can appear to be correct (because 36 is not greater than 42).

However, a check of the local filesystem can see that a file called 42 was written, with the contents 36 .

Attempting to use the reverse comparison (less than), yields a system error:

If numeric comparison is the required operation, -lt and -gt should be used. For more information, see the -gt operator in about_Comparison_Operators.

Display & Redirect Output

On this page I’ll try to explain how redirection works.
To illustrate my story there are some examples you can try for yourself.

For an overview of redirection and piping, view my original redirection page.

Display text

To display a text on screen we have the ECHO command:

This will show the following text on screen:

When I say «on screen», I’m actually referring to the «DOS Prompt», «console» or «command window», or whatever other «alias» is used.

Streams

The output we see in this window may all look alike, but it can actually be the result of 3 different «streams» of text, 3 «processes» that each send their text to thee same window.

Those of you familiar with one of the Unix/Linux shells probably know what these streams are:

  • Standard Output
  • Standard Error
  • Console

Standard Output is the stream where all, well, standard output of commands is being sent to.
The ECHO command sends all its output to Standard Output.

Standard Error is the stream where many (but not all) commands send their error messages.

And some, not many, commands send their output to the screen bypassing Standard Output and Standard Error, they use the Console. By definition Console isn’t a stream.

There is another stream, Standard Input: many commands accept input at their Standard Input instead of directly from the keyboard.
Probably the most familiar example is MORE :

where the MORE command accepts DIR ‘s Standard Output at its own Standard Input, chops the stream in blocks of 25 lines (or whatever screen size you may use) and sends it to its own Standard Output.

(Since MORE ‘s Standard Input is used by DIR , MORE must catch its keyboard presses (the «Any Key») directly from the keyboard buffer instead of from Standard Input.)

Redirection

You may be familiar with «redirection to NUL» to hide command output:

will show nothing on screen.
That’s because >NUL redirects all Standard Output to the NUL device, which does nothing but discard it.

Now try this (note the typo):

The result may differ for different operating system versions, but in Windows XP I get the following error message:

This is a fine demonstration of only Standard Output being redirected to the NUL device, but Standard Error still being displayed.

Redirecting Standard Error in «true» MS-DOS (COMMAND.COM) isn’t possible (actually it is, by using the CTTY command, but that would redirect all output including Console, and input, including keyboard).
In Windows NT 4 and later (CMD.EXE) and in OS/2 (also CMD.EXE) Standard Error can be redirected by using 2> instead of >

A short demonstration. Try this command:

What you should get is:

You see? The same result you got with ECHO Hello world without the redirection.
That’s because we redirected the Standard Error stream to the NUL device, but the ECHO command sent its output to the Standard Output stream, which was not redirected.

Now make a typo again:

What did you get? Nothing
That’s because the error message was sent to the Standard Error stream, which was in turn redirected to the NUL device by 2>NUL

When we use > to redirect Standard Output, CMD.EXE interprets this as 1> , as can be seen by writing and running this one-line batch file «test.bat»:

Now run test.bat in CMD.EXE and watch the result:

It looks like CMD.EXE uses 1 for Standard Output and 2 for Standard Error. We’ll see how we can use this later.

Ok, now that we get the idea of this concept of «streams», let’s play with it.
Copy the following code into Notepad and save it as «test.bat»:

Run test.bat in CMD.EXE, and this is what you’ll get:

Now let’s see if we can separate the streams again.
Run:

and you should see:

We redirected Standard Output to the NUL device, and what was left were Standard Error and Console.

and you should see:

We redirected Standard Error to the NUL device, and what was left were Standard Output and Console.

Nothing new so far. But the next one is new:

and you should see:

This time we redirected both Standard Output and Standard Error to the NUL device, and what was left was only Console.
It is said Console cannot be redirected, and I believe that’s true. I can assure you I did try!

In this case, we could also have used test.bat >NUL 2>NUL
This redirects Standard Output to the NUL device and Standard Error to the same NUL device.
With the NUL device that’s no problem, but when redirecting to a file one of the redirections will lock the file for the other redirection.
What 2>&1 does, is merge Standard Error into the Standard Output stream, so Standard output and Standard Error will continue as a single stream.

Redirect «all» output to a single file:

and you’ll get this text on screen (we’ll never get rid of this line on screen, as it is sent to the Console and cannot be redirected):

You should also get a file named test.txt with the following content:

Note: The commands
test.bat > test.txt 2>&1
test.bat 1> test.txt 2>&1
test.bat 2> test.txt 1>&2
all give identical results.

Redirect errors to a separate error log file:

and you’ll get this text on screen (we’ll never get rid of this line on screen, as it is sent to the Console and cannot be redirected):

You should also get a file named testlog.txt with the following content:

and another file named testerrors.txt with the following content:

Nothing is impossible, not even redirecting the Console output.

Unfortunately, it can be done only in the old MS-DOS versions that came with a CTTY command.

The general idea was this:

A pause or prompt for input before the CTTY CON command meant one had to press the reset button!

Besides being used for redirection to the NUL device, with CTTY COM1 the control could be passed on to a terminal on serial port COM1.

Escaping Redirection (not to be interpreted as «Avoiding Redirection»)

Redirection always uses the main or first command’s streams:

will redirect START ‘s Standard Output to logfile , not command ‘s!
The result will be an empty logfile .

A workaround that may look a bit intimidating is grouping the command line and escaping the redirection:

What this does is turn the part between parentheses into a «literal» (uninterpreted) string that is passed to the command interpreter of the newly started process, which then in turn does interpret it.
So the interpretation of the parenthesis and redirection is delayed, or deferred.

Note: Be careful when using workarounds like these, they may be broken in future (or even past) Windows versions.

A safer way to redirect START ed commands’ output would be to create and run a «wrapper» batch file that handles the redirection.
The batch file would look like this:

How can I redirect console output to file?

I’m new to c. Is there any simple way to redirect all the console’s output (printfs etc.) to a file using some general command line \ linkage parameter (without having to modify any of the original code)?

If so what is the procedure?

4 Answers 4

Use shell output redirection

The standard error will still be output to the console. If you don’t want that, use:

your-command > outputfile.txt 2>&1

You should also look into the tee utility, which can make it redirect to two places at once.

On unices, you can also do:

That way you’ll see the output and be able to interact with the program, while getting a hardcopy of the standard output (but not standard input, so it’s not like a teletype session).

As mentioned above, you can use the > operator to redirect the output of your program to a file as in:

Also, you can append data to an existing file (or create it if it doesnt exit already by using >> operator:

If you really want to learn more about the (awesome) features that the command line has to offer I would really recommend reading this book (and doing lots of programming :))

In Unix shells you can usually do executable > file 2> &1 , whch means «redirect standard output to file and error output to standard output»

Not the answer you’re looking for? Browse other questions tagged c shell io-redirection or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Читайте также:  Создать образ диска для windows для virtualbox
Оцените статью