6

Consider the script "test.ps1:

Get-EventLog -list | gm

When it is called from the Powershell console (".\test.ps1"), it outputs the members as expected.

However, the following scripts don't show the output from "Get-EventLog -list | gm":

Script 1

Get-EventLog -list | gm
break

Script 2

Get-EventLog -list | gm

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Continua execucao"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancela operacao"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice("Title", "Message", $options, 0) 

If I change "Get-EventLog -list | gm" to a text like "Hello World", it is correctly displayed, in both cases.

Why is the output from the cmdlet not shown?

1
  • 3
    A workaroud is to force the output to host: Get-EventLog -list | gm | out-host Commented Jan 15, 2013 at 11:52

1 Answer 1

4

I took a look on some articles on powershell pipelining. The cmdlet used returns a collection of objects, and powershell's default behavior is to forward the output to the screen.

This article explains that the break statement will break the pipeline (in fact, it will break execution until it finds a parent loop), thus explaining the behavior of the first example.

As for the second example, I assume powershell will only default to redirecting to the console on the end of the script execution. As the $host.ui.PromptForChoice does not use the first cmdlet call's output, and produces it's own, the results from Get-EventLog are just discarded.

As it stands, always using "Out-Host", as @Christian said, is the way to go.

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

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.