Do you want to learn everything there is to know about the PowerShell Echo (Write-Output) cmdlet? You’re on the right page!
Overview
The Write-Output cmdlet (echo) writes objects you specify with the InputObject parameter to the pipeline. When a cmdlet writes output to the pipeline, it is also known as the “output stream” or the “success pipeline.”
The “success pipeline” references the information sent down the pipeline when a cmdlet’s command executes successfully. On the other hand, if the command fails, use the Write-Error cmdlet to send error objects to the “error pipeline”.
By default, Write-Output displays the output at the end of a pipeline. For example, in the command below, PowerShell Echo (Write-Output) will display “test output”.
Write-Output "test output"
The reason for this is that this command was not passed to another command through the pipeline (|). However, if I passed the putout to the Get-Member cmdlet, Write-Output will not display “test output”
Write-Output "test output" | Get-Member
Before I move on, I like to mention one more important feature of the Write-Output cmdlet. By default, PowerShell Echo enumerates (treats each item as individual objects) collection objects.
However, if you want to change this default behavior, you can specify the NoEnumerate parameter. When you specify this parameter, Write-Output then treats all the items in the collection as a single object.
There is a very simple way to demonstrate this behavior. If I run the command below:
(Write-Output item1, item2,item3 | Measure-Object).count
The result is 3…
This shows that by default the PowerShell Echo (Write-Output) cmdlet treats each item as an object (enumerates the items in the collection). To change this behavior, I’ll include the NoEnumerate parameter.
(Write-Output item1, item2,item3 -NoEnumerate | Measure-Object).count
Now, the result shows as a single object…
Syntax and Parameters
The echo (Write-Output) cmdlet has a simple syntax. Here it is…
Write-Output [-InputObject] <PSObject[]> [-NoEnumerate] [<CommonParameters>]
The table below lists the parameters of PowerShell Echo (Write-Output) and explains how to use them.
S/N | Parameter Name | PowerShell echo Parameter Meaning/Notes |
---|---|---|
1 | InputObject | Use the InputObject parameter to specify the objects to send down the pipeline. You may enter a variable you saved the objects. Alternatively, you may type a command or expression that gets the objects. |
2 | NoEnumerate | By default, the Write-Output cmdlet always enumerates (lists each item as individual objects) its output. If you want to suppress this default behavior, specify the NoEnumerate parameter. To learn more about this, see my examples section |
3 | <CommonParameters> | CommonParameters is a set of parameters common to many PowerShell cmdlets. These common parameters include PipelineVariable, Verbose, Debug, WarningAction, ErrorAction, ErrorVariable, WarningVariable, OutBuffer, and OutVariable. |
Examples
So far I have covered the basics of this all-important cmdlet. With the basics out of the way, it is time to show you the real deal.
In this section, you’ll learn different examples and ways you can use the Write-Output cmdlet in PowerShell scripting.
1. Write the Output to a Text File
Sometimes you may want to write output to the console and to a text file at the same time. You can use the Write-Output (echo) to do this.
However, since Write-Output cannot directly write to a text file, you have to introduce another cmdlet, Tee-Object.
The logic behind this is that Write-Output will display the text on the console but also sends the putout down the pipeline. Then, Tee-Object will save the output received from Write-Output into the file.
Here is a sample command…
Write-Output item1, item2,item3 | Tee-Object D:\report\Write-Output1.txt
The screenshot below shows the result of the command and the text file.
So, the only cmdlet suitable for this scenario is Tee-Object.
2. Add Line Break to a String
If I run the command below…
echo "this will be displayed in a single line"
PowerShell echo will display the whole string in a single line.
But what if you want to move and add a line break within the string “this will be displayed in a single line”? You can achieve this in two ways.
Firstly, you can break the string where you want to have the line break. Then, run two echo commands, separated by ; character.
echo "this will be displayed"; echo "in a single line"
Alternatively, you can add the new line (`n) character at the point you want to add the line break.
echo "this will be displayed `nin a single line"
The command above produces the same result as the first earlier command…
3. Echo the Current Directory
You do not need the echo command to echo the current directory in PowerShell. To echo the current directory, use the Get-Location command.
Get-Location
The command displays the current directory with the Path property. To get rid of the Path header, call the property with the command below…
(Get-Location).Path
Here is the result of both commands…
4. Avoid Newline
If you need to stop the output of echo from displaying items in a new line, you cannot use Write-Output (or echo). To achieve this, you need to use the Write-Host.
To show you how this works, the command below displays two processes in separate lines.
(Get-Process -Name ACCSvc, AgentSvc).ProcessName
But what if I need to return the two processes in a single line and separate them with a comma? To achieve this, I have to save the output of the last command in a variable (I’ll explain why shortly).
$proc = (Get-Process -Name ACCSvc, AgentSvc).ProcessName
Next, I will use the information saved in the proc variable as the input of the Write-Host command. Then, I will use the Separator parameter of the Write-Host cmdlet to add the comma between the two processes.
Here is the command…
Write-Host $proc -Separator ", "
And here is the result in PowerShell…
5. Append the Output to a Text File
In the first example I shared, I discussed how to write the output of PowerShell echo (Write-Output) to a text file. In that example, I used the code below.
Write-Output item1, item2,item3 | Tee-Object D:\report\Write-Output1.txt
However, in this example, I want to show you how you can append (add) more outputs to the text file Write-Output1.txt. Fortunately, all you need to achieve this is to include the Append parameter in the Tee-Object command.
Write-Output item1, item2,item3 | Tee-Object D:\report\Write-Output1.txt -Append
Here is the result in PowerShell and the updated textile.
6. Output Multiple PowerShell Variables in One Line
I am including this in my example because I found a similar question on stackoverflow.com. In this question, the user pulls user properties from Active Directory and wants to display all three attributes incline (on the same line).
In this instance, you cannot use Write-Output because it will display the attributes in different lines.
To demonstrate this example, I will create three items and save them in three variables.
$a = "service accounts" $b = "user accounts" $c = "machine accounts"
As I mentioned earlier, we cannot write these items with PowerShell Echo (Write-Output). If we used Write-Output, it will display the objects in different lines.
Write-Output $a,$b,$c
So, how do we write the attributes in separate lines? There are many ways to achieve this.
One quick method is by using the -join operator (courtesy of one of the answers in the stackoverflow.com question). Here is how to write the code…
$a, $b, $c -join ','
The code joins the objects and separates them with a comma (,).
Apart from using the -join operator, you can also achieve the same result with Write-Host. Here is the command…
Write-Host $a, $b, $c -Separator ", "
Although this works, it has one limitation – you cannot pipe the output to a text file or a CSV. The reason for this limitation is that, unlike Write-Output, Write-Host does NOT send output to the pipeline.
So, if you wish to write the output to a text file or CSV, you have to fall back to the -join operator method. Here is a command that sends the result of the -join operator command to a text file.
$a, $b, $c -join ',' | Out-file D:\report\multivariabletotext.txt
The screenshot shows the above command in PowerShell. It also shows the information saved on the text file.
7. Write an Empty or Blank Line
You can easily write a blank or empty line with the PowerShell echo command. In this example, I want to include a blank line between the texts “line 1” and “line 2”
echo "line 1" " " "line 2"
The secret is to add two quote characters with a space between them in between the texts. Here is the result in PowerShell…
You can also achieve this result by including the new line character (`n) in between the texts.
Write-Output "line 1""`n" "line 2"
However, by default, the new line character (`n) adds a line break. So, to avoid having double lines between the texts, you have to remove any space between the first text and the new line character (`n).
Meanwhile, if in my command, I add a space between the first text and the new line character (`n), the result will produce double blank lines.
Write-Output "line 1" "`n" "line 2"
If you closely at the screenshot below, you’ll notice that the result of the second command has two blank spaces.
8. Write the Output to a CSV File
In this example, I will show you how to write some information on the screen (echo), then, write the same information to a CSV file.
To demonstrate this, I want to display the result of the Get-Process command on the console. Then, save it on a CSV file.
As with the similar example with the text file, you will need the Tee-Object cmdlet. Here is the command…
Get-Process | Write-Output | Tee-Object -FilePath D:\report\proc-2.csv
And here is the result of the command in PowerShell. The screenshot below also shows the CSV file.
9. Write the Output to a Text File and Change Its Encoding to UTF-8
In this guide, I have discussed many examples where I wrote the output of Write-Output to a text file. However, up till now, I have not shown you how to change the file encoding of the output file.
In previous examples, I used the example code below…
Write-Output item1, item2,item3 | Tee-Object D:\report\Write-Output1.txt
In those examples, I used the LiteralPath (not specified in the last command because it is the default parameter) parameter of Tee-Object. But, what I have not mentioned is that the Tee-Object cmdlet has another parameter called Variable.
So, this means that the Tee-Object cmdlet can either save the output of Write-Object to a file or save it in a variable.
In this example, the command that displays the output of Write-Output to the console, saves it to a text file and modifies the file’s encoding, is in 2 parts.
The first part pipes the output of Write-Output to the Tee-Object command and saves the output of Tee-Object to a specified variable. Then, in the second command, I piped the variable to Out-File, then I use the Encoding parameter of Out-File to specify the file’s encoding as UTF-8.
Here is the full command.
Write-Output item1, item2,item3 | Tee-Object -Variable file; $file | out-file D:\report\proc-utf-8.txt -Encoding utf8
Secondly, you DO NOT pipe the output of the Tee-Object command to the out-file command. Instead, you separate the two commands with a ; character.
Then, pipe the variable (but this time, include the $ character to the variable) to the out-file command
The screenshot below shows the above command in PowerShell as well as the text file. As you can see, the encoding of the text file is UTF-8.
10. Change Display Hashtable in a Different Color
Although this guide is about PowerShell echo (Write-Output), if you want to change the text color of objects in a hashtable, you’ve to use Write-Host instead.
The reason for this is that Write-Output does not have the parameter to change text color.
To demonstrate how to display a hashtable in a different color, I’ll use the code below…
$hashtable = @{ Name = "Document" PSProvider = "FileSystem" Root = "C:\Users\victo\Documents" } $hasstring = $hashtable | Out-String Write-Host $hasstring -ForegroundColor Red
Between lines 1 and 5 I created a hashtable and saved it in the $hashtable variable. Then, in line 6, I converted the objects in the hashtable to strings.
If you do not convert the objects in the hashtable to strings, Write-Host cannot display them correctly.
Finally, in line 7, I used the Write-Host command to display the objects in the hashtable in red.
Here is the script and the result in PowerShell ISE.
Frequently Asked Questions
Echo is one of the aliases of the Write-Output cmdlet. The Write-Output cmdlet writes specified objects to the PowerShell pipeline.
However, if Write-Output is the last command in the pipeline, the objects are displayed in the console as well.
To echo a new line, add the new line character (`n) at the point you wish to write the nee line. For example, if I want to write “Line 2” to the second line in “Line 1 Line 2”, I’ll add the new line character (`n) as shown below:
echo “Line 1 `nLine 2”
To run a multi-line command in PowerShell, use the use semicolon (;) after each command. In one of my examples, I used the command below.
Write-Output item1, item2,item3 | Tee-Object -Variable file; $file | out-file D:\report\proc-utf-8.txt -Encoding utf8
If you look closely, you’ll notice that I have the semicolon (;) after the Tee-Object command. The reason for this is that the two commands are separate commands that I wanted to run in a single line.
You can use the -join operator to join two strings in PowerShell. In addition to just joining two strings, you can also add a separator between the strings.
For example, to join the strings “this is line 1” and “this is line 1”; then separate them with a comma (,), I’ll use the command below:
“this is line 1”, “this is line 1” -join ‘,’
The above command will NOT add a space between the strings (after the comma). If you wish to add a space after the comma, add the space before the last single quote character (‘).
“this is line 1”, “this is line 1” -join ‘, ‘
You can either use Write-Output (echo) or Write-Host to display messages in a PowerShell script.
My Final Thoughts
PowerShell echo or Write-Output is a useful cmdlet for displaying messages in PowerShell. The beauty of the Write-Output cmdlet is that it not only displays messages in the PowerShell console.
In addition to displaying a message on the console, Write-Output also sends the message down the pipeline. The benefit of writing the same message to the pipeline is that you can send that information to other cmdlets via the pipeline.
This is where Write-Output is better than Write-Host. Unlike Write-Output, Write-Host only displays messages on the PowerShell console.
So, you cannot pipe the output of Write-Host down the pipeline.
I hope this post was helpful to you. If it was of any help to you, click on “Yes” beside the “Was this page helpful” question below.
You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom of this page.
Finally, I sincerely feel that you’ll find our other Windows PowerShell Explained articles valuable as well. We also have Windows PowerShell How-To Guides, that offer you more ways to learn PowerShell.