Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I got excited with PowerShell ScriptBlock at first but I was confused recently with its executing ordering inside blocks. For example:

$test_block = {
  write-host "show 1"
  ps
  write-host "show 2"
  Get-Date
}

The output by calling $test_block.Invoke():

show 1
show 2
<result of command 'ps'>
<result of command 'get-date'>

Do commands who output something run first?

share|improve this question
up vote 2 down vote accepted

This behaviour is because write-host doesn't put the output on the pipeline. The other commands are placed on the pipeline so are not output to the screen until the function (invoke) returns.

To get the behaviour I believe you were expecting, use write-output instead, the results of all the commands will then be returned in the pipeline.

$test_block = {
  write-output "show 1"
  ps
  write-output "show 2"
  Get-Date
}

$test_block.Invoke()
share|improve this answer
    
thanks man. that's exactly what i want. – dave Oct 28 '13 at 9:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.