write to console powershell is the operation of sending text, objects, or messages to the PowerShell host. Use Write-Output, Write-Host, or [console]::WriteLine.
Write-Output "Hello, Console"
Syntax
# Write-Output (default cmdlet – sends objects to the success stream)
Write-Output [-InputObject] <PSObject[]> [-NoEnumerate] [<CommonParameters>]
# Write-Host (writes directly to the console host, bypasses pipeline)
Write-Host [[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundColor <ConsoleColor>] [-BackgroundColor <ConsoleColor>] [<CommonParameters>]
# .NET method (low-level, only works in full console host)
[console]::WriteLine("string")
# Example of a custom logging function using Write-Output and file appending
Function Write-Log {
param([string]$Message, [ValidateSet('Info','Warning','Error')][string]$Severity='Info')
$timestamp = Get-Date -Format 'yyyyMMddHHmmss'
$line = "$timestamp,$Severity,$Message"
Write-Output $line
if ($LogToFile) { Add-Content -Path $LogFileName -Value $line }
}
Options and Flags
| Cmdlet/Method | Flag / Parameter | Type | Default | Description |
|---|---|---|---|---|
| Write-Output | -InputObject | PSObject[] | None | The objects to send to the success stream. |
| Write-Output | -NoEnumerate | Switch | False | Suppresses enumeration of a collection. Only effective when piped to a cmdlet that expects a single object. |
| Write-Host | -Object | Object | None | The object to display (converted to string). |
| Write-Host | -NoNewline | Switch | False | Prevents a newline after output. |
| Write-Host | -ForegroundColor | ConsoleColor | Host default | Sets the text color. |
| [console]::WriteLine | N/A | Method | N/A | Writes directly to the console’s output stream. Does not use PowerShell’s formatting system. |
Usage Examples
Example 1: Display process objects using Write-Output
$P = Get-Process -Name powershell
Write-Output $P
Outputs the process objects to the pipeline. If this is the last command, PowerShell automatically calls Out-Default to render them in the console. This method preserves object structure for further pipeline processing.
Example 2: Colored status messages with Write-Host
Write-Host "Starting deployment..." -ForegroundColor Green
# ... deployment steps ...
Write-Host "Deployment complete." -ForegroundColor Green
Write-Host bypasses the pipeline and writes directly to the host UI. Use for user-facing progress, warnings, or errors where formatting is required. Do not use when the output needs to be captured or piped to another cmdlet.
Example 3: Logging with a custom function and file output
Function Write-Log {
param([string]$Message, [string]$Severity='Info', [switch]$LogToFile)
$timestamp = Get-Date -Format 'yyyyMMddHHmmss'
$line = "$timestamp,$Severity,$Message"
Write-Output $line
if ($LogToFile) { Add-Content -Path "C:Logsscript.log" -Value $line }
}
Write-Log -Message "Processing batch #1" -Severity Info -LogToFile
Demonstrates a structured logging approach. The Write-Output command sends the formatted log line to the success stream, while also appending it to a file if the switch is set. Avoid Write-Host in logging functions to preserve the pipeline.
Performance Considerations
For high-volume console output, batch writes using StringBuilder or redirect to a file with Out-File. Write-Output sends objects to the pipeline; Write-Host bypasses it but is slower for large data. The -NoEnumerate flag can reduce overhead when passing collections without unrolling.
Troubleshooting & Common Errors
| Error / Symptom | Root Cause | Resolution |
|---|---|---|
| Write-Output does not produce visible output | Output is being consumed by a downstream cmdlet or variable assignment | Use Write-Output | Out-Host to force display, or inspect pipeline with Tee-Object. |
| Write-Host output is lost in a background job | Jobs run in a non-interactive process; Write-Host writes to the host UI which does not exist | Use Write-Output and capture job output via Receive-Job. |
| [console]::WriteLine throws “System.Console is not supported on this host” | Running in PowerShell ISE or a custom host that does not implement a full console | Prefer Write-Host or Write-Output for portability. |
Formatting issues with -NoEnumerate |
The array is still enumerated; -NoEnumerate only works when piped to a cmdlet that expects a single object |
Use Write-Output $array -NoEnumerate | ForEach-Object { $_ } to see the effect. |
Frequently Asked Questions
What is the difference between Write-Host and Write-Output?
Answer: Write-Host writes directly to console bypassing the pipeline; Write-Output emits objects to the pipeline for subsequent processing.
Write-Host is used for user-facing messages and formatting (e.g., colored output), but it does not return data. Write-Output should be used to pass objects down the pipeline. Example:
# Write-Host: no pipeline output
Write-Host "Message"
# Write-Output: passes string to pipeline
Write-Output "Data" | ForEach-Object { $_ }
When should I use the -NoEnumerate flag with Write-Output?
Answer: Use -NoEnumerate when you need to output a collection as a single object, preventing PowerShell from automatically unrolling it.
Without -NoEnumerate, Write-Output splits an array into separate pipeline items. Useful for passing a whole array to functions expecting one object. Example:
# Unrolls array into two items
Write-Output @(1,2) | Measure-Object | Select-Object Count
# Sends array as single object
Write-Output -NoEnumerate @(1,2) | Measure-Object | Select-Object Count
How do I fix the “Write-Host is deprecated” warning in PowerShell 5.1+?
Answer: Replace Write-Host with Write-Information and set $InformationPreference = ‘Continue’ to display messages to console.
The warning is from PSScriptAnalyzer rule PSUseWriteHost. For production scripts, use Write-Information which supports background colors via -InformationAction. Example:
$InformationPreference = 'Continue'
Write-Information "Info message" -InformationAction Continue
Does Write-Host work on non-Windows platforms like Linux or macOS?
Answer: Yes, Write-Host works cross-platform in PowerShell Core 6+; it is not available in Windows PowerShell on non-Windows.
Windows PowerShell (5.1) is Windows-only. PowerShell Core (6+, now just “PowerShell 7”) runs on Linux and macOS. Write-Host functions identically across platforms, including color support where terminal permits.
What is the fastest way to write to console in PowerShell?
Answer: Use [Console]::WriteLine() for raw output that bypasses PowerShell’s formatting and pipeline overhead.
For high-performance logging or repeated writes, .NET Console methods are significantly faster than Write-Host or Write-Output. Ideal for scripts processing millions of lines. Example:
# Fastest direct console write
[Console]::WriteLine("Speed-critical message")

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.