How to capture output in a variable rather than a logfile?

You have to do:

$test = & $Env:WinDir\system32\inetsrv\appcmd.exe

If you wanted to redirect error as well, add 2>&1 in the end.


Capturing the output of a executable is as simple as,

$cmdOutput = &"Application.exe" 2>&1

2>&1 - Includes the error stream in the output

Return type of the executable in PowerShell is an array of strings. In case of logging such outputs,

Write-Host $cmdOutput

will output the strings in the array to the output stream separated by spaces

To print them in a string per line fashion, choose

Write-Output $cmdOutput

or

$cmdOutput = &"Application.exe" | Out-String
Write-Host $cmdOutput

Tags:

Powershell