In this tutorial, I will explain the differences between Write-Host and Write-Error in PowerShell. As a PowerShell developer, you should know when to use each cmdlet in PowerShell.
Write-Host cmdlet in PowerShell
Write-Host is primarily used to display output directly to the console. This cmdlet is useful for showing messages to the user that are not intended to be part of the script’s output pipeline. For example, you might use Write-Host to display status messages or prompts.
Syntax
Here is the syntax:
Write-Host [-Object] <Object> [-NoNewline] [-Separator <Object>] [-ForegroundColor <ConsoleColor>] [-BackgroundColor <ConsoleColor>] [<CommonParameters>]Examples
Now, let me show you a few examples of using the Write-Host cmdlet in PowerShell.
- Displaying a Message with Custom Colors:
Write-Host "Starting the data import process..." -ForegroundColor GreenHere is the output in the screenshot below:

- Concatenating Strings with a Separator:
$firstName = "John"
$lastName = "Doe"
Write-Host $firstName, $lastName -Separator " "In these examples, Write-Host is used to provide feedback to the user about the script’s progress or to display concatenated strings.
Check out PowerShell: Where-Object vs Select-Object
Write-Error in PowerShell
Write-Error is used to write non-terminating error messages to the error stream. This cmdlet is essential for error handling in your scripts, allowing you to report issues without halting script execution. Errors written with Write-Error can be captured and handled using try/catch blocks.
Syntax
Here is the syntax of Write-Error in PowerShell:
Write-Error [-Message] <String> [-Category <ErrorCategory>] [-ErrorId <String>] [-TargetObject <Object>] [-RecommendedAction <String>] [-PipelineVariable <String>] [<CommonParameters>]Examples
Now, let me show you a few examples of using Write-Error in PowerShell.
- Reporting an Error:
Here is a PowerShell script to report an error.
if (-not (Test-Path "C:\ImportantFile.txt")) {
Write-Error -Message "The file C:\ImportantFile.txt was not found." -ErrorId "FileNotFound" -Category ObjectNotFound
}Here is the exact output in the screenshot below:

- Using Write-Error in a Function:
Here is a PowerShell script to write an error inside a function.
function Get-EmployeeData {
param ($employeeId)
if (-not $employeeId) {
Write-Error -Message "Employee ID is required." -ErrorId "MissingParameter" -Category InvalidArgument
return
}
# Additional logic to retrieve employee data
}In these examples, Write-Error helps to notify users about missing files or parameters, making it easier to diagnose issues.
Check out PowerShell ForEach-Object vs ForEach
Key Differences: Write-Host vs Write-Error
Now, let me show you some differences between Write-Host and Write-Error in PowerShell.
Display vs. Error Handling
- Write-Host: Used for displaying messages directly to the console. It is not intended for error handling and does not affect the script’s output pipeline.
- Write-Error: Used for writing error messages to the error stream. It is essential for error handling and allows scripts to report issues without stopping execution.
Pipeline Impact
- Write-Host: Does not impact the script’s output pipeline. It simply displays messages.
- Write-Error: Sends messages to the error stream, which can be caught and handled using try/catch blocks.
Use Cases
- Write-Host: Ideal for status messages, user prompts, and other non-critical information.
- Write-Error: Best for reporting errors and issues that need to be addressed without terminating the script.
Check out PowerShell Where-Object vs Filter
PowerShell Write-Host vs Write-Error
| Feature | Write-Host | Write-Error |
|---|---|---|
| Purpose | Display messages to the console | Write error messages to the error stream |
| Impact | No impact on the output pipeline | Affects the error stream, can be caught with try/catch blocks |
| Use Cases | Status messages, user prompts | Error reporting, diagnostics |
| Syntax | Write-Host "Message" | Write-Error -Message "Error message" |
| Example | Write-Host "Process started..." -ForegroundColor Green | Write-Error -Message "File not found" -ErrorId "FileNotFound" |
Conclusion
In this tutorial, I explained when and how to use Write-Host and Write-Error in PowerShell. Use Write-Host for displaying console messages and Write-Error for robust error handling in PowerShell. Do you still have any questions? Feel free to leave a comment below.
You may also like:
- PowerShell Write-Host vs Write-Information
- PowerShell Write-Host vs Echo
- PowerShell Write-Host vs Write-Output
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.