Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe output of Invoke-Expression to string?

Tags:

powershell

In a Powershell script, I something like this:

Invoke-Expression "& `"C:\Scripts\psftp.exe`" ftp.blah.com"

I want to pipe all output, errors etc of this to the string $output

How can I do this? I've tried > at the end as well as $output = ... but neither seem to catch errors and the sort.

like image 929
JBurace Avatar asked Sep 07 '12 14:09

JBurace


1 Answers

Try this:

$output = Invoke-Expression "C:\Scripts\psftp.exe ftp.blah.com 2>&1"

The use of the call operator & is unnecessary as is quoting the exe path in this case since the path doesn't contain spaces. If the path contained spaces, then it would need to be quoted and then you would have to use the call operator. That said, I'm not sure why you need to use Invoke-Expression at all in this case. The following would work just as well given your example.

$output = C:\Scripts\psftp.exe ftp.blah.com 2>&1
like image 63
Keith Hill Avatar answered Sep 30 '22 20:09

Keith Hill