What is the PowerShell Echo Equivalent?

The Echo command is a common way to output something to the console when using batch or shell scripts. But in PowerShell, Echo is not an actual cmdlet. So what is the PowerShell equivalent to output something?

When writing scripts, you often need to output something to the console to test or debug your script. In the old batch scripts, for example, you could use Echo for that. But in PowerShell, you have actually multiple options.

In this article, we are going to take a look at the different options in PowerShell.

PowerShell Echo

In PowerShell, you can actually use the Echo command. However, it’s not an actual cmdlet, but an alias for the Write-Output cmdlet.

If we take the example below, then the 4 fruits from the array will be displayed in your console.

$fruitsAvailable = @("Apple", "Banana", "Orange", "Grapes")

$fruitsAvailable | ForEach-Object {
    echo $_
}

But as you might know, in PowerShell you also have the Write-Host cmdlet. And there is an important difference between the Write-Host and the Write-Output that you need to understand.

The Write-Output cmdlet, and thus the alias Echo as well, will write the results to the output stream. Whereas the Write-Host cmdlet will return the results to the host, often your PowerShell console.

We can show the difference with the following examples. The code below will loop through the fruits array and return all the fruits.

$results = $fruitsAvailable = @("Apple", "Banana", "Orange", "Grapes")

$results = $fruitsAvailable | ForEach-Object {
    Write-Host "Processing fruit $($_)"
    $_
}

If you run the code above, then you will get the following in your console:

Processing fruit Apple
Processing fruit Banana
Processing fruit Orange
Processing fruit Grapes

And if you look at the results variable, then you will get a list of all the fruits from the array:

Apple
Banana
Orange
Grapes

But what if we change Write-Host to Write-Output? Write-Output will send the results to the output stream, which in this case is the $results variable. But this can also be a text file when you redirect the output.

$results = $fruitsAvailable = @("Apple", "Banana", "Orange", "Grapes")

$results = $fruitsAvailable | ForEach-Object {
    Write-Host "Processing fruit $($_)"
    $_
}

So if you run the code above, you won’t see anything in the console. However, if we take a look at the $results variable, then we will see the following:

Processing fruit Apple
Apple
Processing fruit Banana
Banana
Processing fruit Orange
Orange
Processing fruit Grapes
Grapes

When to use Write-Output or Write-Host

The Write-Output cmdlet is technically not needed because writing to the output stream is the default behavior of PowerShell.

So if you want to show the progress of your script in the console, or quickly return a variable to test a part of your script, then Write-Host is the best option. The advantage of Write-Host is that you can also assign different colors to the console output.

Another option to keep in mind is the Write-Verbose. The verbose output is hidden by default, but you can change it by setting the variable $VerbosePreference to continue in your PowerShell session (or at the beginning of your script). This way all verbose output will be displayed.

A similar option is the Write-Debug cmdlet, which you can enable with $DebugPreference = "Continue"

PowerShell echo

Wrapping Up

To recap, you can use Echo in PowerShell, but it’s really not needed and there are better options for debugging your code or returning results to the console.

I hope you like this article, if you have any questions, just drop a comment below.

Leave a Comment

0 Shares
Tweet
Pin
Share
Share