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:
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”.
{
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:
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:
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.
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
Write-Output
Echo
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:
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“:
The command will display the members of the System.String class of PowerShell. It means that the string has passed through the pipeline.
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:
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:
$int2=5
$int3=7
echo $int1, $int2, $int3 | Measure-Object
The image below shows the Script pane and the output of the script:
With -NoEnumerate:
$int2=5
$int3=7
echo $int1, $int2, $int3 -NoEnumerate | Measure-Object
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.