Powershell

PowerShell echo command | Explained

In the world of programming languages, printing the output is the primary feature. The PowerShell scripting language does serve the basic fundamentals of any language. The “echo” is a cmdlet used to print any value on the PowerShell console. PowerShell’s Write-Output cmdlet is referred to as the alias of the “echo” command which states that both serve the same purpose.

This post demonstrates the working and usage of the “echo” command in PowerShell.

How to use the PowerShell echo command?

Like other scripting languages, PowerShell also uses the echo command in the script to print the string and other values in the console. The echo command can be used in the following syntax.

Syntax

echo [-InputObject] <psobject[]> [-NoEnumerate]  [<CommonParameters>]

The syntax is described as:

  • InputObject: the parameter determines the objects to send down the pipeline.
  • NoEnumerate: The echo count or enumerates its output by default but it can be disabled by adding the NoEnumerate parameter.
  • CommonParameters: represents the parameter which is used with most of the commands such as Verbose, Debug, ErrorAction

Aliases: Write-Output (can be used in the same way as to echo command)

For a detailed explanation of the echo command, we have demonstrated various examples that are listed below.

Example 1: To Display an Object in Console

The primary use of the echo command is to print the objects in the PowerShell console. For instance, we have printed a line using the echo command as follows:

echo "The echo is used to print the object in the console."

The output states that echo printed the sentence (enclosed in double-quotes) in the PowerShell console.

Example 2: To echo the Value Stored in the Variable

The echo command also prints the value stored in a variable. In the below script we asked the user for their input and at the same time, we stored the user input into the variable called $topic. The echo command then prints the value of the variable.

$topic = Read-Host "Please enter today's topic"
echo "Today's topic of discussion is, $topic"

As per the output, you can see that the user entered the topic of the day and the echo command has printed the value of the variable.

Example 3: Write-Output, an Alias of echo

In PowerShell, the Write-Output is an alias of echo, which means both are used to display the object in the console. In the example code, as given below, we have declared two variables $echo and $writeOuptut for echo and Write-Output respectively.

$echo= echo "The echo is used to print the object in the console."
$writeOutput = Write-Output "The Write-Output is Aliases of echo."

As you can see in the above-attached screenshot, the statements in variables are displayed in the PowerShell console, after calling the $echo and $writeOutput.

Example 4: To Count the Objects using echo Command

In PowerShell, the echo cmdlet contains advanced functionalities. Suppose we want to enumerate or count the values one by one from 1 to 5, use the following echo command to get the results in the console. It is to be noted that echo enumerates its output by default.

echo 1,2,3,4,5 | Measure-Object

The above snip shows that the echo enumerated its values properly and returned output in the console.

As we know, by default the echo enumerates its output, but it can be disabled with the following “-NoEnumerate” parameter.

echo 1,2,3,4,5 -NoEnumerate | Measure-Object

According to the output, it can be seen that the echo command is not passed on to each value one by one. Instead, it considers all the values as “1”.

Good job! You have explored the use of the echo command in PowerShell.

Conclusion

You can use the echo cmdlet to print any values In the PowerShell console. PowerShell provides the Write-Output as an alias of echo that serves the same operation i.e. display the values. In this post, we have explained the core use and functionalities of the echo command in PowerShell.

About the author

Adnan Shabbir