Skip to main content
SysAdmin Shell Scripting Essentials

Write To Console PowerShell Command Reference: Syntax, Flags

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.
See also  mac flushdns: Clear DNS Cache on macOS (All Versions)

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.
See also  vmconnect.exe Hyper-V Virtual Machine Connection CLI Reference

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.