PowerShell print: Output at your fingertips, to screen or file

Printing values or text is a common function we do in programming. This value can be the final output or even an intermediary value to understand if your program is working the way it should. We often print values for debugging and to understand the cause of the error. Regardless of how you use it, print is an important function in any program. In PowerShell, there are multiple options to print your output/values and that’s exactly what we’ll explore in this article.

Printing values to the command-line

PowerShell-Print-

One of the most common ways to check value or output is to print it to the command line or the PowerShell window. There are two commands to do it, namely, Write-Host and Write-Output

You can use it like this:

Write-Host "Hello world!"
Write-Output "Hello world!"

Both commands will print “Hello world!” on the command line.

Write-output parameters

This cmdlet writes information to the next command in the pipeline or the command line if there are no other commands. You can use the following parameters for Write-Output:

  • -InputObject: Specifies the object you want to send to the next command in the pipeline
  • -NoEnumerate: Prevents the cmdlet from enumerating the output

Write-host parameters

This cmdlet is a wrapper for the Write-Information cmdlet and was introduced in PowerShell 5.0. Its task is to write output directly to the information stream. Some of the parameters you can use with it are:

  • -BackgroundColor: Specifies the background color
  • -ForegroundColor: Changes the text color
  • -NoNewLine: No spaces are inserted between the output strings
  • -Separator: Specifies the character that should be used as a separator while printing output

So, what’s the difference between the two commands? To know this difference, let’s understand how PowerShell works when it comes to printing values.

PowerShell’s pipeline

The most powerful aspect of PowerShell is its pipeline mechanics. When you want the output of one command to be passed on to the next, PowerShell does not send the value as a mere text. Rather, it creates an object, encapsulates this value in it, and sends this object in the native format to the next command. This complete mechanism happens within the PowerShell’s objectflow engine.

Now that we have a basic idea of how PowerShell prints values, let’s take a look at the difference between the two commands.

Difference between Write-Host and Write-Output

On the face of it, you may think that both the commands do the same thing. But in reality, they work very differently.

Write-Output sends the value to the PowerShell objectflow engine and in turn, this engine sends the object to the next command. If there are no commands, the value is printed directly to the host.

Let’s see an example here.

Function changeColor
{
process {Write-Host $_ -ForegroundColor Red}
}
Write-Output "Hello world!" | changeColor

powershell-print-foreground

When you execute the above command, the value “Hello world!” will get printed in red because the Write-Output function sends the value through the PowerShell objectflow engine to the method that changes the color.

Now, if you just print…

Write-Output "Hello world!"

…nothing will change and the value is printed to the screen because there are no methods or cmdlets to pipe the output. Hence, the value is printed on the screen directly.

Likewise, when you execute this code…

Write-Host "Hello world!" | changeColor

…you’ll notice that the words “Hello world!” are printed in the default color and not in red and this is where the difference comes in.

In the Write-Host method, the values are not sent to the objectFlow engine, rather they are sent directly to the host, as the name suggests. In this case, all the other instructions are ignored, and the value is printed directly to the screen.

Of course, you have the choice to format the output the way you want in the Write-Host method using the many options that come with it. To change the font color to red, you should write:

Write-Host "Hello world!" -ForegroundColor Red

Understanding this difference is the key to printing values to the command line.

One more example. You can also change the background color with Write-Host:

Function changeColor
{
process {Write-Host $_ -BackgroundColor Red} }
Write-Output "Hello world!" | changeColor 

powershell-print-

Printing values to a file

Many times, you’ll want to print values to a file instead of the command line and you can do that with the cmdlet Out-File.

The nice aspect of this cmdlet is that it creates a new file if no files are existing with the name you specified.

For example:

Get-Process | Out-File -Filepath \testfile.txt

This code will print all the processes to a file called testfile.txt and it will create a new file if there are no files with the same name.

Now, this could be a spot of bother. What if another file exists with the same name and you don’t want your command to overwrite this file? Simply use the parameter called NoClobber.

Get-Process | Out-File -Filepath \testfile.txt -NoClobber

This command will throw an error if a file called testfile.txt already exists. Many parameter options come with the Out-File command and some of them are:

  • -InputObject: Allows you to specify a variable that should be written to the file.
  • -Encoding: To encode output in a specific format such as ASCII, Unicode, UTF7, UTF8, UTF32, and more.
  • -Width: Limits each line in the file to the characters you specify.
  • -Append: Adds value to the end of the file.
  • -Confirm: Confirms with you before proceeding.
  • -FilePath: Gives the choice to specify a particular file path.
  • -Force: Overwrites an existing read-only file.
  • -NoNewLine: Ensures that the file does not end with a newline character.

Thus, these are some of the useful parameters you can run with this cmdlet.

PowerShell print: Screen or file

As you can see, you can print values in PowerShell to either the screen or to a file. In the former, you can use Write-Host or Write-Output, depending on your preferences and your programming flow while for the latter, use Out-File cmdlet.

These cmdlets add to the ease of programming in PowerShell, which just got better with the new PowerShell 7.0 announced by Microsoft recently. Though there is no direct impact on printing values to the host or file, it still comes with many cool features that you can check out.

Featured image: Shutterstock

About The Author

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top