Link to home
Start Free TrialLog in
Avatar of ugeb
ugebFlag for United States of America

asked on

Powershell won't output to file

Hi,

I'm trying to capture a session in Powershell.  I do the following:

D:\coursera-dl-master> Start-Transcript

but this only contains the commands I type and not the output.  If I do "echo 1" I will see both the command and output separately, but if I execute a Python command from a .bat file, I don't see any of its output.

The same happens if I try a redirect like:

D:\coursera-dl-master> ./coursera-dl.bat {options ...}    > out.txt

How can I capture all output in a shell, including from batch scripts and Python programs?
Avatar of FOX
FOX
Flag of United States of America image

go with | out-file c:\pathtofile.txt

put the | at the end of the command with a space then the out-file command as shown above
Avatar of ugeb

ASKER

I'm guessing pathtofile.txt is the output text file.  What is out-file then?

Can you give an example using 'echo' or something?
Out-file sends the results of the command you launched to a file.  

https://technet.microsoft.com/en-us/library/hh849882.aspx
you need to supply a file name parameter with the start-transcript command
Avatar of ugeb

ASKER

Okay, out-file is a program, got it. I got it to work with 'echo'.

I tried this:
D:\coursera-dl-master> ./coursera-dl.bat {options ...}    | out-file out2.txt

The file is empty.  The program coursera-dl.bat is producing errors and I'm trying to catch those, but this technique isn't catching errors.  Is there something special I have to do to catch errors into the file?

@aikimark: You don't need to specify a file, it will use its own file name and tell you what it is when you stop.
Try adding -Append after the out2.txt

If that doesn't work then try this:

D:\coursera-dl-master> ./coursera-dl.bat {options ...} >> out2.txt
Avatar of ugeb

ASKER

Sorry for the late response, I was out of town.

I had tried that 2nd one before, so I tried the append.  Unfortunately neither solution works.

It has something to do with the ability to capture std error text.  It can capture normal output, but not error text.
ASKER CERTIFIED SOLUTION
Avatar of Steven Carnahan
Steven Carnahan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ugeb

ASKER

Yes, that did it, thanks!  Why does adding the '2' make it work?
PowerShell writes its messages to different streams that can be redirected to files for capturing the respective output.
•Stream 1 (default): regular output ("STDOUT")
•Stream 2: error messages ("STDERR"), including error messages from external programs
•Stream 3: warning messages
•Stream 4: verbose messages
•Stream 5: debug messages

To capture a particular stream in a file you need to redirect the stream number to a file name. For instance you wanted error messages so you need to add the 2 to capture Stream 2.