in

WindowsWindows Loving ItLoving It

[LATEST] PowerShell Write-Output and Write-Host difference | Synatx

PowerShell Write or Print to Console shows how to use write-host and write-output to write to the PowerShell console. Easy syntax and examples.

PowerShell write

Let us look at how to print our output the console in PowerShell. There are two different ways to write the output to the PowerShell and they are write-host and write-output.

The syntax of using them is shown below.

Jump to:

People also search for:

PowerShell Write:

Write-Host “hello Teckangaroo.com”

write host powershell

In the above syntax, be sure to replace the text with the necessary text that you want to use.  We can also print the output using write-output using the below syntax.

Write-Output “hello Teckangaroo.com”

write output powershell

Let us see how to replace the plain text with a variable and print it to the console.

$variable = “hello teckangaroo.com”

write-host $variable

write-output $variable

The above script will produce the output as shown in the image as shown below.

write variable powershell

Difference between write-host and write-output:

Even though they seem to produce the same output, their use and behavior is very different.  This is because PowerShell is capable of passing the output of one script to another using the pipe.  For example, consider the script to get the processes.

gps

get process

The output for this is shown in the image and it is not very useful for everyone.  If we want to see only the name of the processes, we can pipe the output and select only the name as shown below.

gps | select name

pipe output powershell

The output of the above script is shown in the image and it is clear that the ability to pipe the output of one script to another is very useful.

But it is not always that we want to pipe the output of the script.  Sometimes we just want to print the output to the console.  When we want to just print the output to the console, we use the write-host and when we want to pipe the output to another script, we use the write-output.

To show this we will use the following example.

We will write two functions and each will just return an array of numbers from 1 to the input number.  The script for the functions are given below.

function write-host-array($num){
write-host $(1..$num)
}

write host

The above function is using write-host to write to the output.

function write-output-array($num){
write-output $(1..$num)
}

write output

This function is using write-ouptput to write to the output.

Let us try to pipe the output of either of the functions to get an array of even numbers.  We will first call this on the write-host-array and expect only a normal array for the output because it does not allow the output to be piped like write-output.

write-host-array 5 | %{$_ * 2}

The output of the above script is shown in the image and clearly it does not return any even number array.

write host

If we try the same process on the write-output-array we will get an even number array.  The below scripts is and example.

write-output-array 5 | %{$_ * 2}

The output of the above script is shown in the image below.

output powershell

As expected we are able to produce the even number array.

Let us see what happened here. The write-host is capable of only writing the input to the host.  It is the default function that is called when there is something that can be displayed.  That means, in the commandlet that let us print the processes to the console, the write-host is called in the end for us to write the desired content to the host.

write-host is called in write-output also but it is called when there are no more scripts in the pipeline.  This makes sense to call the write-host in the end because there are no more scripts in the pipeline and also write-host is not capable of passing the output the next member in the pipeline.

Verdict:

If you are planning on writing a function that does not have to pass the output to the script using the pipe, use the write-host.  And if you have a function that needs to pass the output to the next function, use the write-output.  The potential place where we can use write-output is the Fibonacci series.

If you want us to show the Fibonacci series function, please let us know in the comments below.   For more awesome posts, stay tuned to TecKangaroo.

PowerShell Write or Print to Console
  • Article Rating
  • Content Relevance
  • Content Clarity
  • Author Rating
4.5

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments