Powershell

How to Print an Output in PowerShell

Printing output is the essential requirement while dealing with any programming language. The purpose of printing the output is manifold. Users can print the output for debugging purposes or to show the result of any program.
PowerShell also provides support for scripting languages, which have all the basic features of a programming language. Keeping in view the importance of printing, this post lists various methods to print an output in PowerShell.

Method 1: Using Write-Output Cmdlet to Print Output in PowerShell

One of the common methods to check the value or print output in PowerShell is Write-Output. This is the primary method that most PowerShell users exercise. This method can be used in the scripts as well as in the PowerShell terminal to print strings and any other objects on the PowerShell console. Let’s practice it using the following lines of code:

$User = Read-Host -Prompt 'INPUT THE NUMBER'
   IF ($User -lt 30) {
          Write-Output "VALUE $User IS LESS THAN 30."
      }
   ELSEIF ($User -eq 50) {
          Write-Output "VALUE $User IS EQUAL TO 50."
   }
   ELSE {
     Write-Output "VALUE $User IS NOT GREATER THAN 30 NOR EQUAL TO 50"
     }

In the above code, the “Write-Output” is used to display information by checking the condition after input from the user in PowerShell. The if else-if statement is used and in code statements of each block, the Write-Output cmdlet is used.

Output

The output shows that the Write-Output has printed the line on the console.

Method 2: Using Write-Verbose to Print an Output in PowerShell

One of the methods to display a stream message in PowerShell is known as Write-Verbose. By default, the stream of verbose messages is not shown in the PowerShell console. However, a keyword is used at the end of the message to display information named “-Verbose”.

Code

Write-Host "Using the Verbose command in PowerShell"
Write-Verbose -Message "Welcome to PowerShell" -Verbose

In the above code, the first line simply gives the information of the user. In the second line, use the Write-Verbose method to give a stream of message information to the user in the PowerShell console.

Output

The above display highlights the text with yellow font color and a black background. This method is used to display a large message or information to the console in PowerShell.

Method 3: Using Write-Warning to Print an Output in PowerShell

In PowerShell, one method to display or print a warning message is Write-Warning. The difference from Write-Output is that it does not generate any output. Write-Warning is used to display a prompt or warning message that highlights the text. Moreover, users can use the $WarningPreference variable for further action in the PowerShell console.

Code

Write-Warning "Welcome to PowerShell"
The code is used to display a warning or a prompt message to the user using the Write-Warning method.

Output

The highlighted output represents a warning message “Welcome to PowerShell” in the console window.

Method 4: Using Write-Host to Print an Output in PowerShell

The method of Write-Host is used specifically to display messages in different colors based on the user’s needs. In this method, the user manually changes the background and foreground colors in PowerShell using the keywords -BackgroundColor and -Foregroundcolor, respectively.

Code

Write-Host "Welcome to PowerShell"

Use the Write-Host method to display messages in different colors. It directly sends commands to the host, bypassing all other instructions.

Output

The outcome shows a message “Welcome to PowerShell” using the Write-Host method in PowerShell.

Method 5: Using Write-Debug to Print an Output in PowerShell

The objective of using the Write-Debug method is to print a debug message from the command or script to the PowerShell console. The objective of utilizing this method is to identify errors and display them based on developer needs. The debug messages are not shown in the console by default, but $debugPreference can be utilized to present messages in PowerShell. The below code validates the display of a message.

Code

Write-Debug "Not print a message of error on 1st line."
 $debugPreference = "Continue"
Write-Debug "Print a message of error on 3rd line "

The code for debugging the commands is written as above. Using this code, the user can display an error message or not according to their needs.

Output

The output shows that the first line is not printed on the PowerShell console, while the third line is printed, which shows the “Print a message of error on the 3rd line” message using $debugPreference.

Conclusion

PowerShell supports numerous methods to print output on the console or to get the output in a file. These methods include Write-Output, Write-Host, Write-Verbose, Write-Warning, and Write-Debug. Every method has its own reason to use it. Like, the Write-Warning is used to print specific output as a warning. Similarly, the Write-Verbose is used to write the output in detail. This post has demonstrated the working and usage of all these methods to print output in PowerShell.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.