Home » How to Echo Variable in PowerShell

How to Echo Variable in PowerShell

In PowerShell, you can output the value of a variable using various methods.

Method 1: Using the Variable Directly to Echo Variable Value

$variable = "Hello, Adam!"
$variable

This example prints the variable value to the console directly.

Method 2: Using the echo Command to Print Variable Value

echo "Hello, Adam!"

The echo command is a Window DOS command an alias of Write-Output cmdlet used to output or display value to the console.

Method 3: Using the Write-Output cmdlet to Print Variable

Write-Output "Hello, Adam!"

This example displays the output to the console.

Method 4: Using the Write-Host cmdlet to Output Variable Value

$name = "Hello, Adam!"
Write-Host $name

This example displays the output of a variable to the console directly.

All of these methods can be utilized to output variable values to the console.

The following examples show how to use each of the methods.

Using the Variable Directly to Echo Variable Value

The simplest way to output a variable’s value to the console is to use the variable directly.

The following example shows how to use it.

$name = "Adam"

$name

PowerShell automatically outputs the value of the $name variable when we use it on a new line.

The output of the above PowerShell is:

Adam

Using the echo Command to Print Variable Value

The echo command is a Windows DOS command and alias of Write-Output cmdlet. It is used to print the value of a variable to the console.

The following example shows how to do it.

$name = "Adam"
echo "Hello, $name"

In this example, $name stores the name. The echo command prints the value of a variable $name onto the console.

The output of the above script is:

Hello, Adam

Using the Write-Output cmdlet to Print Variable

The Write-Output is the commonly used cmdlet in PowerShell to output the value of a variable.

The following example shows how to do it.

$name = "Adam"
Write-Output "Hello, $name"

The output of the above script prints the value of a variable $name to the console.

Hello, Adam

Using the Write-Host cmdlet to Output Variable Value

The Write-Host cmdlet can be used to output a variable’s value to the console directly.

The following example shows how to use it with syntax.

$name = "Hello, Adam"
Write-Host $name

The output of the above script displays the value of a variable $name to the terminal directly.

Hello, Adam

Conclusion

I hope the above article on how to echo the variable value in PowerShell is helpful to you.

Using the variable directly or the Write-Output cmdlet are the most common methods to output a variable in PowerShell.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.