1

If I have a very simple script that contains a function that just does the following:

function saySomething() {
   Write-Output "Saying someting..."
}

If I call this function from the console the output is displayed. If I, however, do the following:

$something = saySomething

Then no output is displayed. Obviously my working example is much more complicated than this, but in short, how do I display the output within a function if that function is used to set a variable?

5
  • Just write out the variable after you set it. any reason for not doing that? Commented Oct 1, 2013 at 14:45
  • @MusaabAl-Okaidi the variable is actually a complex object. It's a slow running function and I want to display a certain status as its building out the object. Commented Oct 1, 2013 at 14:46
  • Have you experimented with Tee-Object? This will allow you to output to the console and a file at the same time, but if you're not outputting simple text then I guess this wouldn't work for you as you'd need to read the object back to the variable right? Commented Oct 1, 2013 at 14:51
  • just realized that Tee-Object can output to a variable so see my answer below. Commented Oct 1, 2013 at 14:54
  • @inquisitor - If you want to display a status, have a look at the Write-Progress cmdlet. If your function both returns the thing it's building and the status, the variable you assign the result to will have status messages that might not be useful. Commented Oct 1, 2013 at 23:14

1 Answer 1

3

Try the following:

saySomething | Tee-Object -Variable something

This should work exactly as you want.

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.