This post will overview the methods to write output to the PowerShell Console.
How to Write to the Console in PowerShell?
These cmdlets can be utilized to write to the PowerShell console:
Method 1: Write to the Console in PowerShell Using “Write-Output” Command
The “Write-Output” command is primarily used to output the text input to the console. This command sends the specified objects to the output stream.
Syntax
Example 1: Using “Write-Output” Command to Display a Message in PowerShell
In this example, we will output the added text by utilizing the “Write-Output” command:
Example 2: Using “Write-Output” Command Inside the Function in PowerShell
For the second scenario, we will add the same command within a function like this:
Write-output "This is a sample text"
}
text-output
In the above code:
- First, we have defined a function named “text-output”.
- After that, we added the “Write-Output” command to write the text to the PowerShell console.
- Finally, call the function by writing its name outside the body:
The text has been written in the PowerShell ISE console.
Method 2: Write to the Console in PowerShell Using “Write-Host” Command
Another command used to write to the PowerShell console is the “Write-Host”. This cmdlet is used to output the text input to the PowerShell console. Moreover, it can be utilized for customizing the output text.
Example 1: Using “Write-Host” Command to Display a Message in PowerShell
Now, we will write the customized text to the PowerShell console:
In the above code, we have used the “Write-Host” command to write the text to the PowerShell console. Moreover, the “-BackgroundColor” parameter is added to change the background color of the text to black:
Example 2: Using “Write-Output” Command Inside the Function in PowerShell
This example will output the customized text to the PowerShell ISE console:
Write-Host "Hello People" -BackgroundColor Yellow
}
output-text
In the above code:
- Define a function and then added the text using the “Write-Host” command.
- Use the “-BackgroundColor” parameter to change the text color to yellow.
- Finally, call the function with its name:
As you can observe, the customized text has been written in the PowerShell ISE console.
Conclusion
To write to the console in PowerShell, there are various methods, but the majorly used ones include the “Write-Output” and the “Write-Host” command. Both cmdlets take the input as text and output to the console. However, the Write-Host can also customize the added text. This post has explained practical methods to resolve the mentioned query.