The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output. The syntax of the echo command in PowerShell is shown below:

Syntax:

The syntax given above will print everything written inside the double quotes. If you have declared a variable and want to print its value, type the variable inside double quotes, and its value will be displayed. However, if you use single quotes in the echo command, only the variable name will be shown instead of its value.

This article will provide a detailed overview of PowerShell cmdlet “Write-Output” and the difference between “Write-Host“, “echo“, and “Write-Output“.

First, we will start with the differences among the following three commands of PowerShell: Write-Host, Write-Output, and Echo.

Difference Between Echo, Write-Host, and Write-Output

In PowerShell, echo and Write-Host generate the same output. However, Write-Host differs from echo command and Write-Output cmdlet in returning values to the PowerShell engine. Write-Output is used as an alias to the echo command. You can execute the command given below to get the “Alias” of the echo command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image1-19.png" data-lazy- height="250" src="data:image/svg xml,” width=”948″>

The difference is that “Write-Host” only writes on the host and does not return any value to the PowerShell engine. On the contrary, “Write-Output” writes on the screen and returns the content to the “PowerShell” engine. We will explain the difference between “Write-Output“, “Echo” and “Write-Host” with examples:

Example 1

We have created a PowerShell script named “printcmdlet” and have written the code in it. We have made a function “difference” and specified the text color and background color using flags “-ForegroundColor” and “-BackgroundColor“, respectively. You can observe that we have piped the “difference” function with all three printing commands, “echo“, “Write-Output“, and “Write-Host”.

function difference

{

    process {Write-Host $_ -ForegroundColor yellow -BackgroundColor black}

}

echo “this is a test” | difference

Write-Host “this is a test” | difference

Write-Output “this is test” | difference

Write-Host “this is a test” -ForegroundColor black -BackgroundColor White

The script view of the code is given below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image3-18.png" data-lazy- height="164" src="data:image/svg xml,” width=”645″>

To run the script, jump to the directory where you have saved the script. Once you are there, write “.“, this symbol will open a dropdown menu that shows all the files inside the directory. Find your script, and once it is loaded, press “F5” to run:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image2-19.png" data-lazy- height="197" src="data:image/svg xml,” width=”507″>

Note: the “echo” and “Write-Output” have fetched the function. However, “Write-Host” failed to do so. You have to change the characteristics of “Write-Host” as we did in the 8th line of the script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image5-14.png" data-lazy- height="148" src="data:image/svg xml,” width=”575″>

Example 2

We will explain another example that will enlighten the difference between “echo”, “Write-Host”, and “Write-Output”:

We have created three scripts and named PO_Host, PO_Out and PO_echo, for “Write-Host”, “Write-Output”, and “echo”, respectively. In these scripts, we have created a variable in which the “Get-Command” cmdlet is saved. The “Get-Command” command is used to print all the Alias, Functions, and PowerShell cmdlets on the system.

Write-Host

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image4-15.png" data-lazy- height="284" src="data:image/svg xml,” width=”602″>

Write-Output

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image7-13.png" data-lazy- height="485" src="data:image/svg xml,” width=”707″>

Echo

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image6-15.png" data-lazy- height="430" src="data:image/svg xml,” width=”712″>

Example 1 shows that only the “Write-Host” does not pipe the function, and in Example 2, the Write-Host cmdlet provides the result but is not in order. The difference explained in the above examples depicts that “echo” and “Write-Host” can be used alternatively, but “Write-Host” can be used only when you want to get the output on the console.

Use of Echo in PowerShell

The echo command is used to display the output on the output console of PowerShell ISE.

For instance, you want to print “Hello World” using the echo command in PowerShell. First, create a PowerShell script, and we have named it “hello.ps1“. Open the script, and write the following code:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image9-9.png" data-lazy- height="264" src="data:image/svg xml,” width=”414″>

The echo command can also be used to pass the output to another PowerShell cmdlet. The following code will pipe the “pass output” string to another PowerShell cmdlet, “Get-Member“:

> echo “pass output” | Get-Member

The command will display the members of the System.String class of PowerShell. It means that the string has passed through the pipeline.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image8-10.png" data-lazy- height="349" src="data:image/svg xml,” width=”684″>

However, there are other multiple operations that we can perform using the echo command in PowerShell, such as using parameters to manipulate the output as per your requirements.

Echo supports various parameters, as shown below. Type “Write-Output” in the scripting pane of the “PowerShell ISE“, and then write hyphen (-). A dropdown menu will be activated, which contains the supported parameter:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image12-6.png" data-lazy- height="225" src="data:image/svg xml,” width=”543″>

For instance, the echo/Write-Output command prints the output as an individual expression. The -NoEnumerate parameter helps you to get a single count of the terms used in the echo command:

Let’s explain this parameter with the help of an example:

We have taken three integers and printed them using “echo” in a PowerShell script: we are piping a PowerShell cmdlet “Measure-Object“. This PowerShell cmdlet will count the total number of expressions used in an echo command, and it will return 3 as a count. If we use    “-NoEnumerate“, then it will display 1 in the count field. We will explain it using the “-NoEnumerate” and without using “-NoEnumerate“.

Without -NoEnumerate:

$int1=3

$int2=5

$int3=7

echo $int1, $int2, $int3 | Measure-Object

The image below shows the Script pane and the output of the script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image10-9.png" data-lazy- height="335" src="data:image/svg xml,” width=”491″>

With -NoEnumerate:

$int1=3

$int2=5

$int3=7

echo $int1, $int2, $int3 -NoEnumerate | Measure-Object

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image11-6.png" data-lazy- height="366" src="data:image/svg xml,” width=”516″>

Conclusion

Like other scripting languages, PowerShell supports echo command to print anything which is written inside double-quotes. Also, there are other printing commands in PowerShell: Write-Output and Write-Host. The echo and Write-Output have the same working procedure, while Write-Host is different from both. In this tutorial, we have provided a brief explanation of the echo command in PowerShell. Moreover, a clear difference between “Write-Host“, “Write-Output“, and “echo” is provided to understand which command will suit you.