0

I'm starting to learn (beginner) about PowerShell script. I found that echo and Write-Host both can display output in the console screen, so what is the difference between them? I read this question from Stack Overflow, but I am unable to understand the answers of it. I want to know the differences in simple terms.

1
  • Take a look at start-transcript as well. – spikey_richie Mar 26 at 9:42
0

I will try to explain it to you more philosophically than technically.

  • Write-Host is used to show information in the console. It does not get written into the output stream
  • echo, which is actually an alias of Write-Output is used to output Objects of an operation

Take the following example into consideration:

This code here:

$Results = 0..10 | ForEach-Object {
    Write-Host "Current Number: $_"
    $_
}

Will output the following in the console:

Current Number: 0
Current Number: 1
Current Number: 2
Current Number: 3
Current Number: 4
Current Number: 5
Current Number: 6
Current Number: 7
Current Number: 8
Current Number: 9
Current Number: 10

And if you check the Variable $Results now, you can see this:

PS C:\WINDOWS\system32> $Results
0
1
2
3
4
5
6
7
8
9
10

The numbers are saved into the Variable, because $_ on line no. 3 was sent to the output stream. Write-Host however, is sent to the host, hence it's displayed in the console, but not in the variable because it creates nothing in the output stream.

What happens now if we change Write-Host to Write-Output?:

$Results = 0..10 | ForEach-Object {
    Write-Output "Current Number: $_"
    $_
}

If we run this, we won't see anything in the console, because the whole output stream got saved in the $Results Variable. If we check this Variable now it looks like this:

PS C:\WINDOWS\system32> $Results
Current Number: 0
0
Current Number: 1
1
Current Number: 2
2
Current Number: 3
3
Current Number: 4
4
Current Number: 5
5
Current Number: 6
6
Current Number: 7
7
Current Number: 8
8
Current Number: 9
9
Current Number: 10
10

So as you see, if you want to print simple information, that really has no further use than just informing you what the code is currently doing, you use Write-Host

If you want to output anything to the output stream, you use Write-Output

But hold on! There is more.

You don't actually need Write-Output. I write PowerShell Scripts for about 5, 6 years now, and not once have I used it. Why? Because writing to the output stream is the default in PowerShell. If you look at our examples again, the $_ on line three always magically got into the output stream, even though I did not use Write-Output.

So to summarize: use Write-Host to display information in the console and I would suggest don't use echo/Write-Output because it's the default anyway.

0

As said, Write-Output A.K.A echo outputs the objects

Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console.

So if you see some texts on the screen then it's just because your command is the last in the pipeline. But each object is printed on a separate line so the biggest difference you can easily see is that echo won't print it's arguments in the same line

PS C:\Users> echo This is a string
This
is
a
string
PS C:\Users> Write-Host This is a string
This is a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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