Stew C
Stew C

Reputation: 758

PowerShell: Run batch file and write output to text file

I have a batch file in my PowerShell script that I want to run and write its output to a text file. I tried the following:

Start-Process C:\path\file.bat | Out-File C:\path\output.txt

I also tried using Tee-Object and Write-Output in place of Out-File, but nothing is being written to the text file.

Upvotes: 2

Views: 12089

Answers (3)

Major Malfunction
Major Malfunction

Reputation: 113

You just needed a couple more parameters:

Start-Process -FilePath 'C:\Path\File.bat' -RedirectStandardOutput 'C:\Path\Output.txt' -Wait -WindowStyle Hidden

-RedirectStandardOutput to send output to your text file, -Wait to wait for the batch file to complete and -WindowStyle Hidden to hide the window used for the batch file.

Upvotes: 0

oɔɯǝɹ
oɔɯǝɹ

Reputation: 7625

You can use Start-Transcript and Stop-Transcript to let the PowerShell console write all console output to a text file.

Use it like this:

Start-Transcript -Path C:\temp\log.txt
&C:\path\file.bat
Stop-Transcript

See the documentation for more details.

Upvotes: 3

Rab
Rab

Reputation: 61

If you want the output of the bat file, e.g. results of a build script, then I found the following worked best:

$Product = 'ProductName' 
&C:\Path\$Product.bat | Out-File C:\Path\$Product-Output.txt

Upvotes: 0

Related Questions