Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - No output from write-output

Reply
 
Old 04-10-2007   #1 (permalink)
DouglasWoods
Guest


 
 

No output from write-output

I have this script, which does not run as I expected.
----------------------------------------
function foo($bar)
{
write-output "I got $bar"
}

foo "Hello"
measure-command {foo "World"}
---------------------------------------

Here is the ouput.

---------------------------------------
I got Hello


Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 1
Ticks : 15418
TotalDays : 1.78449074074074E-08
TotalHours : 4.28277777777778E-07
TotalMinutes : 2.56966666666667E-05
TotalSeconds : 0.0015418
TotalMilliseconds : 1.5418
--------------------------------------------------

Why didn't I get a line saying
I got World

Douglas Woods

My System SpecsSystem Spec
Old 04-10-2007   #2 (permalink)
Marcel J. Ortiz [MSFT]
Guest


 
 

Re: No output from write-output

> Why didn't I get a line saying
> I got World


Because write-output doesn't write to the console. It sends objects down
the pipeline which measure-command will then discard. measure-object will
only output the TimeSpan object for how long it took. write-host will write
to the console.
So if you change write-output to write-host you'll get what you expected.


"DouglasWoods" <DouglasWoods@discussions.microsoft.com> wrote in message
newsE5409AF-3797-48C3-9462-7E80D105081E@microsoft.com...
>I have this script, which does not run as I expected.
> ----------------------------------------
> function foo($bar)
> {
> write-output "I got $bar"
> }
>
> foo "Hello"
> measure-command {foo "World"}
> ---------------------------------------
>
> Here is the ouput.
>
> ---------------------------------------
> I got Hello
>
>
> Days : 0
> Hours : 0
> Minutes : 0
> Seconds : 0
> Milliseconds : 1
> Ticks : 15418
> TotalDays : 1.78449074074074E-08
> TotalHours : 4.28277777777778E-07
> TotalMinutes : 2.56966666666667E-05
> TotalSeconds : 0.0015418
> TotalMilliseconds : 1.5418
> --------------------------------------------------
>
> Why didn't I get a line saying
> I got World
>
> Douglas Woods


My System SpecsSystem Spec
Old 04-11-2007   #3 (permalink)
mikes.net
Guest


 
 

Re: No output from write-output

On Apr 10, 8:03 pm, "Marcel J. Ortiz [MSFT]"
<mos...@online.microsoft.com> wrote:
> > Why didn't I get a line saying
> > I got World

>
> Because write-output doesn't write to the console. It sends objects down
> the pipeline which measure-command will then discard. measure-object will
> only output the TimeSpan object for how long it took. write-host will write
> to the console.
> So if you change write-output to write-host you'll get what you expected.


In my opinion outputting text in Powershell is too tricky. There are
various quirks. For example, using Write-Host in a script causes
naked text to stop being output. Format-XXX can't be used inside
scripts. Etc. This whole area could use some documentation. Someone
needs to write a "Powershell Gotchas" article.

But the very worst thing about Powershell is when you have to work on
a machine that doesn't have it.

Mike

My System SpecsSystem Spec
Old 04-13-2007   #4 (permalink)
June Blender (MSFT)
Guest


 
 

Re: No output from write-output

Mike, these are only "gotchas" if they're not clearly explained. Please use
http://connect.microsoft.com to file doc bugs. If you can include an e-mail
address, I'd be happy to let you review the fixes.

--
June Blender [MSFT]
Windows PowerShell Documentation
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"mikes.net" wrote:

> On Apr 10, 8:03 pm, "Marcel J. Ortiz [MSFT]"
> <mos...@online.microsoft.com> wrote:
> > > Why didn't I get a line saying
> > > I got World

> >
> > Because write-output doesn't write to the console. It sends objects down
> > the pipeline which measure-command will then discard. measure-object will
> > only output the TimeSpan object for how long it took. write-host will write
> > to the console.
> > So if you change write-output to write-host you'll get what you expected.

>
> In my opinion outputting text in Powershell is too tricky. There are
> various quirks. For example, using Write-Host in a script causes
> naked text to stop being output. Format-XXX can't be used inside
> scripts. Etc. This whole area could use some documentation. Someone
> needs to write a "Powershell Gotchas" article.
>
> But the very worst thing about Powershell is when you have to work on
> a machine that doesn't have it.
>
> Mike
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Write-Output is bleeding into a pipe why PowerShell
Formatting output with write-host PowerShell
write-output array without C/R PowerShell
Duplicating import-csv with write-output PowerShell
write whatif output to file PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46