0

I have a script which when it runs, prints out lines to the terminal (with errors). I would like to redirect this output into files.

I read that I should add this line in my PS1 script: ".\myscript.ps1 *> &1 > outfile.log" but it doesn't work because of ampersand character is not allowed.

I.T Delinquent
  • 2,305
  • 2
  • 16
  • 33
Walid Laasmi
  • 11
  • 1
  • 2
  • There are lots of ways to do this, one option would be to use Start-Transcript and Stop-Transcript which would output everything that the script does. – I.T Delinquent Jul 09 '19 at 09:24
  • Try the answers here: [How to redirect the output of a PowerShell to a file during its execution?](https://stackoverflow.com/questions/1215260/how-to-redirect-the-output-of-a-powershell-to-a-file-during-its-execution) – techguy1029 Jul 09 '19 at 15:22

2 Answers2

1

You don't need space symbol before ampersand.

.\myscript.ps1 *>&1 will redirect all output streams to the stream number 1

.\myscript.ps1 *>outfile.log will redirect all output streams to the file

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6

Vladimir Dronov
  • 1,182
  • 9
  • 11
0

If you just want to put your lines into another file from a terminal I would try this: ./myscript.ps1 2>outfile.log "./" activates the script "2>" takes the stderr stream into (in this case) the outfile.log But I am not sure what you are supposed to do. Maybe if you could post your code or your task here that would be great.

if you want both streams in one outlog, I would do it like that "2>&1"