Reputation: 40573
I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:
.\MyScript.ps1 > output.txt
How do I redirect the output of a PowerShell script during its execution?
Upvotes: 280
Views: 815318
Reputation: 4606
This is what worked for me:
.\myscript.ps1 -myarg1 arg1 -myarg2 arg2 *>&1 1> output.txt
Here is an explanation:
*>&1
redirect all streams (specified by *
) to the success stream1>
redirect the success stream (specified by 1
) to a fileRefer to the documentation to see all the possible options: about Redirection.
It's a bit more complicated than my liking, but I guess it's more flexible.
Upvotes: 2
Reputation: 5090
If you want a straight redirect of all output to a file, try using *>>
:
# You'll receive standard output for the first command, and an error from the second command.
mkdir c:\temp -force *>> c:\my.log ;
mkdir c:\temp *>> c:\my.log ;
Since this is a straight redirect to file, it won't output to the console (often helpful). If you desire the console output, combined all output with *&>1
, and then pipe with Tee-Object
:
mkdir c:\temp -force *>&1 | Tee-Object -Append -FilePath c:\my.log ;
mkdir c:\temp *>&1 | Tee-Object -Append -FilePath c:\my.log ;
# Shorter aliased version
mkdir c:\temp *>&1 | tee -Append c:\my.log ;
I believe these techniques are supported in PowerShell 3.0 or later; I'm testing on PowerShell 5.0.
Upvotes: 32
Reputation: 1140
I take it you can modify MyScript.ps1
. Then try to change it like so:
$(
Here is your current script
) *>&1 > output.txt
I just tried this with PowerShell 3. You can use all the redirect options as in Nathan Hartley's answer.
Upvotes: 46
Reputation: 153
If you want to do it from the command line and not built into the script itself, use:
.\myscript.ps1 | Out-File c:\output.csv
Upvotes: 11
Reputation: 4165
Microsoft has announced on Powershell's Connections web site (2012-02-15 at 4:40 PM) that in version 3.0 they have extended the redirection as a solution to this problem.
In PowerShell 3.0, we've extended output redirection to include the following streams:
Pipeline (1)
Error (2)
Warning (3)
Verbose (4)
Debug (5)
All (*)
We still use the same operators
> Redirect to a file and replace contents
>> Redirect to a file and append to existing content
>&1 Merge with pipeline output
See the "about_Redirection" help article for details and examples.
help about_Redirection
Upvotes: 70
Reputation: 4223
Maybe Start-Transcript
would work for you. First stop it if it's already running, then start it, and stop it when done.
$ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null $ErrorActionPreference = "Continue" Start-Transcript -path C:\output.txt -append # Do some stuff Stop-Transcript
You can also have this running while working on stuff and have it saving your command line sessions for later reference.
If you want to completely suppress the error when attempting to stop a transcript that is not transcribing, you could do this:
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue" # or "Stop"
Upvotes: 256
Reputation: 494
To embed this in your script, you can do it like this:
Write-Output $server.name | Out-File '(Your Path)\Servers.txt' -Append
That should do the trick.
Upvotes: 0
Reputation: 8684
You might want to take a look at the cmdlet Tee-Object. You can pipe output to Tee and it will write to the pipeline and also to a file
Upvotes: 17
Reputation: 29450
One possible solution, if your situation allows it:
Create a new MyScript.ps1 that looks like:
.\TheRealMyScript.ps1 > output.txt
Upvotes: 27