How to Use Echo in PowerShell

August 8, 2023

Introduction

The echo command in Windows PowerShell is used to print text to the console. The command is an alias for the Write-Output cmdlet.

This article helps you understand how to use echo in PowerShell and learn the differences between echo, Write-Output, and Write-Host.

How to use Echo in Powershell

Write-Output and Echo in PowerShell

The echo utility is a Windows legacy command from the DOS operating system, still supported in Windows for compatibility reasons. 

The main echo function in the Command Prompt (CMD) is to print a message to the console as long as the message is in quotation marks. The echo syntax is:

echo ["message"]

For instance, run the following command:

echo "This is a sentence." 
CMD output of echo

The output prints the This is a sentence message. 

Note: Learn how to use the echo command in Linux.

PowerShell is a more powerful tool than CMD and uses cmdlets to perform tasks. One cmdlet is Write-Output, which displays output in the console. The echo command is used as an alias for Write-Output, so the two commands are interchangeable in many cases.

Both echo and Write-Output have numerous features:

  • Text printing.
  • Object printing.
  • Formatting the output using the -Format cmdlets.
  • Specifying the output encoding.
  • Redirecting the output to a file.
  • Parameters that allow users to customize the output.

Example of echo in PowerShell

Both echo and Write-Output have the same functionality and flexibility. Still, many users prefer Write-Output for better clarity and readability in scripts with advanced functions.

The basic echo output in PowerShell is similar to the one in CMD:

echo "This is another sentence."
Echo PowerShell output

The command prints the text in the output. However, echo offers additional options regarding formatting the output and working with different parameters. For example, set the parameter $name:

$name = "Sara"
Set variable $name

Next, run echo on the variable:

echo $name
Run echo on the variable

The output prints the content of the variable.

echo also supports formatting the output by piping it to a Format- cmdlet.

Available cmdlets are:

  • Format-Custom. Customizes the output with different views.
  • Format-Hex. Shows data in hexadecimal format.
  • Format-List. Creates lists of objects' properties.
  • Format-Wide. Depicts a single object's property spanning across the screen.
  • Format-Table. Presents the data in the form of a table.

For instance, to format the output into a table, run the following:

echo "Number", "Name", "Age", "Location" | Format-Table
echo format table PowerShell output

The echo command is also useful in functions. For instance, write a function to test if a number is a prime number:

Function CheckIfNumberIsPrime($num) {
if ($num -lt 2) {
        echo "$num is not a prime number"
    }
    else {
        $isPrime = $true
        for ($i = 2; $i -lt $num; $i++) {
            if ($num % $i -eq 0) {
                $isPrime = $false
                break
            }
        }

        if ($isPrime) {
            echo "$num is a prime number"
        }
        else {
            echo "$num is not a prime number"
        }
    }

}

To test the function, run:

CheckIfNumberIsPrime($num)

Replace ($num) with a prime number, for example:

CheckIfNumberIsPrime(7)
Test echo function prime numbers

The output confirms seven is a prime number. Next, try 135:

CheckIfNumberIsPrime(135)
Test echo function in a PowerShell and it's not a prime number

The output verifies 135 is not a prime number.

Note: Check out our breakdown of differences between PowerShell and CMD.

Example of Write-Output in PowerShell

The Write-Output cmdlet is a powerful command which prints text, objects, and formatted output to the console.

The basic Write-Output syntax is:

Write-Output [object]

The object can be text, numbers, arrays, etc. The Write-Output usage examples are the same as echo.

For instance, run the following:

Write-Output "This is another sentence."
Write-Output PowerShell

Using a function with Write-Output does not differ from echo. The function in the following example is the PrimeNumber? function. It is the same as the earlier one, designed to identify prime numbers:

Function PrimeNumber?($num) {

     if ($num -lt 2) {
         Write-Output "$num is not a prime number"
     }
     else {
         $isPrime = $true
         for ($i = 2; $i -lt $num; $i++) {
             if ($num % $i -eq 0) {
                 $isPrime = $false
                 break
             }
         }

         if ($isPrime) {
             Write-Output "$num is a prime number"
         }
         else {
             Write-Output "$num is not a prime number"
         }
     }
Write-Output function in PowerShell

Test the function with the number seven by running:

PrimeNumber?(7)
Test Write-Output prime number function

Next, test with 135:

PrimeNumber?(135)
Test Write-Output prime number function with no prime number confirmed

The outputs confirm seven is a prime number while 135 is not.

Note: Learn how to use Command Prompt by utilizing our list of Windows CMD commands.

Write-Host vs. Echo

Write-Host is a PowerShell cmdlet that prints text to the console for display purposes only. It does not return any value to the PowerShell engine. The primary purpose is to produce for-(host)-display-only output, such as printing colored text when prompting the user for input.

Write-Output, on the other hand, writes data to the PowerShell pipeline. The output is displayed on the screen if there is no subsequent command in the pipeline to receive this data.

A key Write-Host advantage is the customization capability. Write-Host supports a broader range of formatting options.

To specify the output's foreground and background colors, use:

Write-Host "Text" -ForegroundColor [Color] -BackgroundColor [Color]

For instance, print a line This sentence is white with a magenta background:

Write-Host "This sentence is white with a magenta background" -ForegroundColor White -BackgroundColor Magenta
Write-Host color chnage

The output is displayed in the requested format.

Conclusion

After reading this article, you know how to use echo, Write-Output, and Write-Host in PowerShell.

Next, learn how to set environment variables in Windows.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Install winget (Windows Package Manager)
April 21, 2022

winget is an open-source Windows package manager designed by Microsoft for Windows 10 and Windows 11. This tutorial shows how to install and use...
Read more
How to Install and Configure SMTP Server on Windows
March 1, 2023

This tutorial shows how to install and configure an SMTP server on Windows operating systems. Use SMTP for email exchange on the Internet....
Read more
chkdsk Command with Examples
January 5, 2023

chkdsk is a Windows utility that scans disks on the system and reports on the state of the stored data. This tutorial shows you how to use chkdsk to scan and repair your Windows drives...
Read more
How to Install Windows 11 on Hyper-V Virtual Machine
February 10, 2022

If you want to experience the new Windows 11 before you decide to use it as your daily driver, install it inside a virtual machine. This tutorial...
Read more