Redirecting Output in Powershell

J

Jasmin

Hi,

I would like to redirect Powershell output to standard output.
Does Powershell redirect its output by default to standard output?
Is there any parameter I need to indicate in my command in order to do so?

Thanks,
Jasmin
 

My Computer

H

Hal Rottenberg

Jasmin wrote:

> I would like to redirect Powershell output to standard output.
> Does Powershell redirect its output by default to standard output?
Yes.

> Is there any parameter I need to indicate in my command in order to do so?
No.

However, I'm not sure that this answers your questions. What are you trying to do?

--

Hal Rottenberg
Blog: http://halr9000.com
Webmaster, Psi (http://psi-im.org)
Co-host, PowerScripting Podcast (http://powerscripting.net)
 

My Computer

J

Jeffery Hicks [MVP]

You can continue to use > and >> to redirect console output. But using a
cmdlet like Out-File is preferred. There is also the Tee-Object cmdlet
which will let you send output to a file and the console.

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.


"Jasmin" <[email protected]> wrote in message
news:[email protected]

> Hi,
>
> I would like to redirect Powershell output to standard output.
> Does Powershell redirect its output by default to standard output?
> Is there any parameter I need to indicate in my command in order to do so?
>
> Thanks,
> Jasmin
 

My Computer

J

Jasmin

Thanks Hal.

I'm trying to invoke Powershell cmdlets through Java/C and extracting its
output for further processing. I'm not interested in redirecting the out to a
file.
The problem was I wasn't able to get the output of my command (for instance:
PowerShell.exe -Command "&{write-host "Test"}" )
The only command I managed to get its output was: PowerShell.exe –help.

Additional information regarding the Powershell output mechanism could be
very useful: How does it work? Does it buffer the output until some limit is
reached or flush it in the end of the command, for example?


"Hal Rottenberg" wrote:

> Jasmin wrote:

> > I would like to redirect Powershell output to standard output.
> > Does Powershell redirect its output by default to standard output?
>
> Yes.
>

> > Is there any parameter I need to indicate in my command in order to do so?
>
> No.
>
> However, I'm not sure that this answers your questions. What are you trying to do?
>
> --
>
> Hal Rottenberg
> Blog: http://halr9000.com
> Webmaster, Psi (http://psi-im.org)
> Co-host, PowerScripting Podcast (http://powerscripting.net)
>
 

My Computer

J

Jeffery Hicks [MVP]

This: PowerShell.exe -Command "&{write-host "Test"}" will work fine from a
CMD prompt. But I think you are really missing something important.
PowerShell is object oriented not a text based shell. At the very end of an
expression you get a textual representation of the objects that remain in
the pipeline. PowerShell has a cmdlet called Out-default that processes the
end of the pipeline. It sends output to Out-Host which handles the
formatting and data presentation to the console.

Or because PowerShell is based on .NET, why not call those classes directly
from Java or C? (I'm not a developer but I do know PowerShell).

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.
"Jasmin" <[email protected]> wrote in message
news:[email protected]

> Thanks Hal.
>
> I'm trying to invoke Powershell cmdlets through Java/C and extracting its
> output for further processing. I'm not interested in redirecting the out
> to a
> file.
> The problem was I wasn't able to get the output of my command (for
> instance:
> PowerShell.exe -Command "&{write-host "Test"}" )
> The only command I managed to get its output was: PowerShell.exe –help.
>
> Additional information regarding the Powershell output mechanism could be
> very useful: How does it work? Does it buffer the output until some limit
> is
> reached or flush it in the end of the command, for example?
>
>
> "Hal Rottenberg" wrote:
>

>> Jasmin wrote:

>> > I would like to redirect Powershell output to standard output.
>> > Does Powershell redirect its output by default to standard output?
>>
>> Yes.
>>

>> > Is there any parameter I need to indicate in my command in order to do
>> > so?
>>
>> No.
>>
>> However, I'm not sure that this answers your questions. What are you
>> trying to do?
>>
>> --
>>
>> Hal Rottenberg
>> Blog: http://halr9000.com
>> Webmaster, Psi (http://psi-im.org)
>> Co-host, PowerScripting Podcast (http://powerscripting.net)
>>
 

My Computer

J

Jasmin

Thanks Jeffery.

I am aware of the fact PowerShell is object oriented and not text based.
The question is how does this fact affect invoking cmdlets from my Java/C
program?
Is there any difference between a cmdlet called from CMD prompt and a cmdlet
called from a Java/C program regarding getting the objects textual
representation?
Out-Default and Out-Host cmdlets: to the best of my knowledge, PowerShell
adds them automatically. Is this correct only for CMD prompt?


"Jeffery Hicks [MVP]" wrote:

> This: PowerShell.exe -Command "&{write-host "Test"}" will work fine from a
> CMD prompt. But I think you are really missing something important.
> PowerShell is object oriented not a text based shell. At the very end of an
> expression you get a textual representation of the objects that remain in
> the pipeline. PowerShell has a cmdlet called Out-default that processes the
> end of the pipeline. It sends output to Out-Host which handles the
> formatting and data presentation to the console.
>
> Or because PowerShell is based on .NET, why not call those classes directly
> from Java or C? (I'm not a developer but I do know PowerShell).
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
> "Jasmin" <[email protected]> wrote in message
> news:[email protected]

> > Thanks Hal.
> >
> > I'm trying to invoke Powershell cmdlets through Java/C and extracting its
> > output for further processing. I'm not interested in redirecting the out
> > to a
> > file.
> > The problem was I wasn't able to get the output of my command (for
> > instance:
> > PowerShell.exe -Command "&{write-host "Test"}" )
> > The only command I managed to get its output was: PowerShell.exe –help.
> >
> > Additional information regarding the Powershell output mechanism could be
> > very useful: How does it work? Does it buffer the output until some limit
> > is
> > reached or flush it in the end of the command, for example?
> >
> >
> > "Hal Rottenberg" wrote:
> >

> >> Jasmin wrote:
> >> > I would like to redirect Powershell output to standard output.
> >> > Does Powershell redirect its output by default to standard output?
> >>
> >> Yes.
> >>
> >> > Is there any parameter I need to indicate in my command in order to do
> >> > so?
> >>
> >> No.
> >>
> >> However, I'm not sure that this answers your questions. What are you
> >> trying to do?
> >>
> >> --
> >>
> >> Hal Rottenberg
> >> Blog: http://halr9000.com
> >> Webmaster, Psi (http://psi-im.org)
> >> Co-host, PowerScripting Podcast (http://powerscripting.net)
> >>
>
 

My Computer

J

Jeffery Hicks [MVP]

Are you invoking your programs from the CMD shell or PowerShell? Although
it would seem in your case all you are going to work with is text.
Everything should eventually come through StdOut.

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.

"Jasmin" <[email protected]> wrote in message
news:[email protected]

> Thanks Jeffery.
>
> I am aware of the fact PowerShell is object oriented and not text based.
> The question is how does this fact affect invoking cmdlets from my Java/C
> program?
> Is there any difference between a cmdlet called from CMD prompt and a
> cmdlet
> called from a Java/C program regarding getting the objects textual
> representation?
> Out-Default and Out-Host cmdlets: to the best of my knowledge, PowerShell
> adds them automatically. Is this correct only for CMD prompt?
>
>
> "Jeffery Hicks [MVP]" wrote:
>

>> This: PowerShell.exe -Command "&{write-host "Test"}" will work fine from
>> a
>> CMD prompt. But I think you are really missing something important.
>> PowerShell is object oriented not a text based shell. At the very end of
>> an
>> expression you get a textual representation of the objects that remain in
>> the pipeline. PowerShell has a cmdlet called Out-default that processes
>> the
>> end of the pipeline. It sends output to Out-Host which handles the
>> formatting and data presentation to the console.
>>
>> Or because PowerShell is based on .NET, why not call those classes
>> directly
>> from Java or C? (I'm not a developer but I do know PowerShell).
>>
>> --
>> Jeffery Hicks
>> Microsoft PowerShell MVP
>> http://www.scriptinganswers.com
>> http://www.powershellcommunity.org
>>
>> Now Available: WSH and VBScript Core: TFM
>> Coming Soon: Windows PowerShell: TFM 2nd Ed.
>> "Jasmin" <[email protected]> wrote in message
>> news:[email protected]

>> > Thanks Hal.
>> >
>> > I'm trying to invoke Powershell cmdlets through Java/C and extracting
>> > its
>> > output for further processing. I'm not interested in redirecting the
>> > out
>> > to a
>> > file.
>> > The problem was I wasn't able to get the output of my command (for
>> > instance:
>> > PowerShell.exe -Command "&{write-host "Test"}" )
>> > The only command I managed to get its output was: PowerShell.exe –help.
>> >
>> > Additional information regarding the Powershell output mechanism could
>> > be
>> > very useful: How does it work? Does it buffer the output until some
>> > limit
>> > is
>> > reached or flush it in the end of the command, for example?
>> >
>> >
>> > "Hal Rottenberg" wrote:
>> >
>> >> Jasmin wrote:
>> >> > I would like to redirect Powershell output to standard output.
>> >> > Does Powershell redirect its output by default to standard output?
>> >>
>> >> Yes.
>> >>
>> >> > Is there any parameter I need to indicate in my command in order to
>> >> > do
>> >> > so?
>> >>
>> >> No.
>> >>
>> >> However, I'm not sure that this answers your questions. What are you
>> >> trying to do?
>> >>
>> >> --
>> >>
>> >> Hal Rottenberg
>> >> Blog: http://halr9000.com
>> >> Webmaster, Psi (http://psi-im.org)
>> >> Co-host, PowerScripting Podcast (http://powerscripting.net)
>> >>
>>
 

My Computer

K

Karl Prosser[MVP]

Ok, so you are not calling powershell.exe , you are using java via com
to host the powershell engine?

2 things

1) if you are calling powershell.exe with something, the output you will
get is plain console output, and should be no different than calling any
other console application

2) if you somehow manage to host powershell in java via com, then WOW..
in that case you'll probably get some sort of com proxy objects wrapping
the actual collection of objects being returned by powershell.
 

My Computer

Top