Home » PowerShell » PowerShell echo equivalent command – Write-Output

PowerShell echo equivalent command – Write-Output

The PowerShell echo command is used to print strings, text, or variables to the console or to a file. The PowerShell echo alias command is Write-Output.

You can use the PowerShell echo command as given:

echo "Echo Equivalent in PowerShell!"

The output of the above echo command in PowerShell displays text to the console.

PowerShell echo command
PowerShell echo command

echo command prints the string or text passed as an argument.

You can use other PowerShell cmdlets like Write-Output, Write-Host, Write-Verbose, and Write-Debug for echo in PowerShell.

In this article, we will discuss using the PowerShell echo command and its equivalent echo alias in PowerShell.

Let’s practice!

PowerShell echo command Syntax

The basic syntax for the PowerShell echo command is:

echo [argument]

For example,

echo "Everything you should know about PowerShell echo"

echo in PowerShell prints string or text written inside the double quotes.

Cool Tip: How to concatenate string in PowerShell!

Using PowerShell echo to Display Text on Screen

To display the text to the console using the PowerShell echo command, type the text that you want to display after the echo command.

echo "Hello, Welcome to ShellGeek!"

In the above PowerShell script, the echo command prints the text “Hello, Welcome to ShellGeek!” to the screen.

PowerShell echo variable to print variable to console

The PowerShell echo command can be used to print variable to the console. It prints the value of a variable to the console.

To display variable value to the console using the PowerShell echo command, store the value in the variable and use the variable name after the echo command.

For example,

$strMsg = "Ultimate guide to PowerShell echo variable!"

echo "$strMsg" 

In the above example of PowerShell echo variable print, the $strMsg contains the value of the string data type.

Using the echo variable inside the double quotes, it prints the variable value on the console.

The output of the above command is:

PowerShell echo variable
PowerShell echo variable

Cool Tip: If you use a variable inside a single quote, it prints a variable name. Hence always use double quotes.

Let’s practice the PowerShell echo equivalent command!

Cool Tip: How to exit script in PowerShell!

Using PowerShell echo to File

The PowerShell echo command can be used to redirect output to a file.

To perform echo to file in PowerShell, use the echo command and type the text that you want to output, and provide the file name after the text.

echo "Hello, Welcome to ShellGeek!" > welcome.txt

This command will create a file “welcome.txt” and write the output “Hello, Welcome to ShellGeek!” to the file.

PowerShell echo Variable to File

Using the PowerShell echo command, you can redirect the value of a variable to the file.

To perform PowerShell echo variable to file, store the text “Hello World” in the variable called $tmp. Use the echo command that takes variable name $tmp and file name called “welcome.txt” as follows.

$tmp = "Hello World"
echo $tmp > welcome.txt

This command creates a file called “welcome.txt” and writes the value of a variable “Hello World” to the file.

Using Write-Output as PowerShell echo equivalent

PowerShell Write-Output cmdlet is an alias or PowerShell echo equivalent command. It writes the output of the content to the console.

Syntax to use the Write-Output command

Write-Output [-InputObject] [-NoEnumerate] []
PowerShell echo equivalent Write-Output
PowerShell echo equivalent Write-Output

Let’s practice with an example.

Write-Output "PowerShell echo equivalent command is Write-Output!"

The output of the above command is

PowerShell echo equivalent command is Write-Output!

Cool Tip: How to remove a user from the group in PowerShell!

Using Write-Host to print the content

The Write-Host command in PowerShell is used to print the content to the PowerShell console.

You can use customization in Write-Host like applying foreground color, background color, and font color for the parameter in the command.

Write-Host "PowerShell echo equivalent command : Write-Host" -ForegroundColor green -BackgroundColor black

In the above example,

The Write-Host command applies customization like foreground color and background color to parameters and prints it on the PowerShell console.

Write-Host PowerShell echo equivalent
Write-Host PowerShell echo equivalent

Cool Tip: How to use $null in PowerShell!

Use of PowerShell echo command in Function

You can use PowerShell echo or Write-Output in function to print the contents of a variable.

Let’s practice with an example.

Create a function to check number is even or odd. If the number is even, print the message to the console using Write-Output.

If the number is odd, use PowerShell echo to print the message.

Function CheckNumberIsEven($num) {

    if($num % 2 -eq 0) {
        Write-Output "$num is even"
        }
     else{
        echo "$num is odd"
     }

}

CheckNumberIsEven(4)

PowerShell echo and Write-Output commands in PowerShell print the value to the PowerShell console.

Cool Tip: How to Keep PowerShell Console Open using the PowerShell NoExit switch!

Conclusion

I hope the above article about PowerShell echo and its equivalent Write-Output is helpful to you.

PowerShell echo prints the argument passed to it on the console.

You can also print variable content using the PowerShell echo variable.

Write-Output is an alias for PowerShell echo to write the output message to the PowerShell console.

Cool Tip: Using Active Directory UserAccountControl flags in PowerShell!

Write-Host is also used to write the output message to the PowerShell console.

It provides customization for output messages (like you can change font color, foreground color, or background color).

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