How to Write Output in Windows PowerShell
-
the
Write-Output
Cmdlet in PowerShell -
the
Write-Host
Cmdlet in PowerShell -
the
Write-Debug
Cmdlet in PowerShell -
the
Write-Verbose
Cmdlet in PowerShell
Writing output to the console is an essential process in any language as it correctly gives feedback to the user. However, there are multiple ways of printing output in Windows PowerShell. This article will differentiate the multiple Write
cmdlets and provide situations on when and where we can use them.
Before we get into the discussion of cmdlets, it is worth noting that Windows PowerShell can output something by enclosing a single line with double quotation marks (""
).
Example Code:
"Hello World!"
Output:
Hello World!
This syntax is possible in Windows PowerShell due to the string literal expression and hidden pipeline. The syntax is equivalent to the below example code.
Example Code:
"Hello World!" | Out-Host
On the other hand, the Out-Host
cmdlet sends the objects preceded for display.
the Write-Output
Cmdlet in PowerShell
The first printing method in Windows PowerShell is the Write-Output
cmdlet. This cmdlet is the basic syntax of printing inside our PowerShell scripting environment. We can equate it to many language’s basic printing commands such as print
, and stdout
.
Example Code:
Write-Output "Hello World!"
Output:
Hello World!
the Write-Host
Cmdlet in PowerShell
The Write-Host
cmdlet is another printing method similar to the previous way, Write-Output
. The only difference is that it can output different colors using the parameters -BackgroundColor
and -ForegroundColor
.
the Write-Debug
Cmdlet in PowerShell
The Write-Debug
cmdlet is also another method for printing in Windows PowerShell. However, this is usually used more for printing debug messages in the scripting environment. The messages are not displayed by default but can be displayed using the $debugPreference
variable.
Example Code:
Write-Debug "Error on line 1 but will silently continue."
$debugPreference = "Continue"
Write-Debug "Error on line 3 will now be displayed"
Output:
DEBUG: Error on line 3 will now be displayed
the Write-Verbose
Cmdlet in PowerShell
The Write-Verbose
cmdlet writes text to the verbose message stream in Windows PowerShell. The verbose message stream is defined to deliver more information about command processes. Like Write-Debug
, the verbose message is not displayed by default but can be displayed using the variable $VerbosePreference
or by adding the switch parameter -Verbose
.
Example Code:
Write-Verbose -Message "This will not be displayed."
Write-Verbose -Message "This will be displayed" -Verbose
Output:
VERBOSE: This will be displayed
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn