6

I have a foreach loop inside my powershell script with prints $output on the shell during each iteration. There are lot many outputs and the number of entries that the shell can display is limited. I am looking to export the output to a text file. I know how to do it in command line. How is it possible in a powershell though?

FYI, I am using a batch script from the command line to run the powershell script as

powershell c:\test.ps1 c:\log.log 

2 Answers 2

14

You can always redirect the output an exe to a file like so (even from cmd.exe):

powershell c:\test.ps1 > c:\test.log

Within PowerShell, you can also redirect individual commands to file but in those cases you probably want to append to the log file rather than overwrite it e.g.:

$logFile = 'c:\temp\test.log'
"Executing script $($MyInvocation.MyCommand.Path)" > $logFile
foreach ($proc in Get-Process) {
    $proc.Name >> $logFile
}
"Another log message here" >> $logFile

As you can see, doing the redirection within the script is a bit of a pain because you have to do lots of redirects to file. OTOH, if you only want to redirect part of the output to file then you have more control this way. Another option is to use Write-Host to output info to the console meant for someone observing the results of the script execution. Note that Write-Host output cannot be redirected to file.

This is an example executed from CMD.exe

C:\Temp>type test.ps1
$OFS = ', '
"Output from $($MyInvocation.MyCommand.Path). Args are: $args"

C:\Temp>powershell.exe -file test.ps1 1 2 a b > test.log

C:\Temp>type test.log
Setting environment for using Microsoft Visual Studio 2008 Beta2 x64 tools.
Output from C:\Temp\test.ps1. Args are: 1, 2, a, b
Sign up to request clarification or add additional context in comments.

4 Comments

So if I am having an argument to the powershell script, can I just simply do: powershell c:\test.ps1 c:\log.log > c:\test.log where log.log is the argument?
Yes, but try using the -File parameter e.g. powershell -file c:\test.ps1 c:\log.log > c:\test.log
if I try using file parameter (or without file) in the batch file I get the following line being executed in the command line: powershell -file c:\test.ps1 c:\log.log 1>c:\test.log I dont understand how that "1" comes there..... And I guess the whole text after the script name is taken to be the input file name and hence the output is not what is expected. And this behaviour is the same whether I use "-file" or not.
@ssn I'm not sure why you are seeing the 1>. I updated the answer to show how this is working for me.
5

what about using the 'tee' command

C:\ipconfig | tee C:\log.txt

1 Comment

This is what I was looking for. I wanted to output to both screen and to file. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.