Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 454 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exporting powershell output to text file

#1
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
Reply

#2
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
Reply

#3
what about using the 'tee' command

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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through