4

I need to capture the output created by a .bat file that is run from within a .ps1 file.

I'm new to powershell scripting but I know this isn't correct syntax. This best illustrates what I need to have happen.

I need to execute the facter.bat script then store its output in $Body so I can use that text later.

 "C:\Program Files (x86)\Puppet Labs\Puppet\bin\facter.bat" > $Body

2 Answers 2

9

This should do it:

$Body = & "C:\Program Files (x86)\Puppet Labs\Puppet\bin\facter.bat"

Sign up to request clarification or add additional context in comments.

2 Comments

It works, but there's a problem with this solution. Doing it this way only contains the standard output, but not the standard error too. Is there a way we can capture that too?
@PiyushSoni: 2>&1
1

If you need to copy the output directly to another file, you can use the following command

& "C:\Program Files (x86)\Puppet Labs\Puppet\bin\facter.bat" | Out-File -FilePath "<FilePathToWrite>.txt"

Comments

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.