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.
People also search for:
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.
Let us see how to replace the plain text with a variable and print it to the console.
The above script will produce the output as shown in the image as shown below.
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.
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.
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.
The above function is using write-host to write to the 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.
The output of the above script is shown in the image and clearly it does not return any even number array.
If we try the same process on the write-output-array we will get an even number array. The below scripts is and example.
The output of the above script is shown in the image below.
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.
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