How to output something in PowerShell
Categories:
Mastering Output in PowerShell: Displaying Data Effectively

Learn the fundamental commands and techniques for displaying information, objects, and formatted data in PowerShell, from simple text to structured output.
PowerShell is a powerful scripting language and command-line shell for Windows, Linux, and macOS. A core part of using PowerShell effectively is understanding how to output information. Whether you're debugging a script, displaying results to a user, or piping data to another command, mastering output techniques is crucial. This article will guide you through the primary cmdlets and methods for displaying data in PowerShell.
Basic Output: Write-Host and Write-Output
The simplest ways to output information in PowerShell are Write-Host
and Write-Output
. While they both display text, they serve different purposes. Write-Host
is primarily for displaying messages directly to the console, often with formatting, and is not typically used for piping data. Write-Output
, on the other hand, sends objects to the success stream, making them available for other cmdlets in the pipeline.
Write-Host "Hello, PowerShell! This is a console message."
Write-Host "This message is" -ForegroundColor Green -BackgroundColor DarkGray
"This is an object sent to the output stream." | Write-Output
Write-Output "Another object for the pipeline."
# Example of piping Write-Output
Write-Output "Hello" | Select-Object @{Name='Message'; Expression={$_}}
Using Write-Host for console messages and Write-Output for pipeline data.
Write-Output
(or simply letting an expression resolve) is preferred over Write-Host
. Write-Host
should be reserved for user-facing messages that don't need to be processed further.Structured Output: Format-Table, Format-List, and Format-Wide
PowerShell works extensively with objects, not just raw text. When you get data from a cmdlet (like Get-Service
or Get-Process
), it returns objects with properties. The Format-*
cmdlets are essential for displaying these object properties in a readable, structured way. They are 'end-of-pipeline' cmdlets, meaning they typically come last in a command chain as they format objects for display, not for further processing.
# Display services in a table format
Get-Service | Format-Table -Property Name, Status, DisplayName -AutoSize
# Display a single service's properties as a list
Get-Service -Name 'Spooler' | Format-List -Property *
# Display process names in a wide, multi-column format
Get-Process | Format-Wide -Property ProcessName -Column 4
Examples of using Format-Table, Format-List, and Format-Wide for structured output.
flowchart TD A[Get-Service] --> B{Objects} B --> C[Format-Table] B --> D[Format-List] B --> E[Format-Wide] C --> F["Formatted Table Output"] D --> G["Formatted List Output"] E --> H["Formatted Wide Output"] style A fill:#f9f,stroke:#333,stroke-width:2px style B fill:#ccf,stroke:#333,stroke-width:2px style C fill:#bbf,stroke:#333,stroke-width:2px style D fill:#bbf,stroke:#333,stroke-width:2px style E fill:#bbf,stroke:#333,stroke-width:2px style F fill:#afa,stroke:#333,stroke-width:2px style G fill:#afa,stroke:#333,stroke-width:2px style H fill:#afa,stroke:#333,stroke-width:2px
Flow of objects through formatting cmdlets in PowerShell.
Exporting Data: Export-Csv, ConvertTo-Json, and Out-File
Beyond just displaying data in the console, you often need to export it to files for reporting, further processing, or integration with other systems. PowerShell provides cmdlets like Export-Csv
for comma-separated values, ConvertTo-Json
for JSON format, and Out-File
for generic text output.
# Export service information to a CSV file
Get-Service | Select-Object Name, Status, StartType | Export-Csv -Path ".\services.csv" -NoTypeInformation
# Convert a custom object to JSON format
$myObject = [PSCustomObject]@{ Name = "Test"; Value = 123; Active = $true }
$myObject | ConvertTo-Json -Depth 3 | Out-File -FilePath ".\data.json"
# Output simple text to a file
"This is a line of text." | Out-File -FilePath ".\log.txt" -Append
"Another line." | Out-File -FilePath ".\log.txt" -Append
Exporting data to CSV, JSON, and plain text files.
Export-Csv
, always consider adding the -NoTypeInformation
switch. Without it, PowerShell adds a #TYPE System.Management.Automation.PSCustomObject
header to the CSV, which can interfere with parsing by other applications.Advanced Output: Write-Verbose, Write-Debug, and Write-Error
For script development and debugging, PowerShell offers specialized output cmdlets that don't interfere with the normal success stream. Write-Verbose
provides detailed information that can be enabled or disabled, Write-Debug
outputs messages for debugging purposes, and Write-Error
generates non-terminating errors.
function Test-OutputTypes {
param(
[Parameter(Mandatory=$true)]
[string]$InputString
)
Write-Verbose "Starting function Test-OutputTypes with input: '$InputString'"
if ($InputString -eq "debug") {
Write-Debug "Debug mode activated. Input was 'debug'."
}
if ([string]::IsNullOrEmpty($InputString)) {
Write-Error "InputString cannot be null or empty."
return
}
Write-Output "Processed input: $($InputString.ToUpper())"
Write-Verbose "Function Test-OutputTypes completed."
}
# To see verbose/debug messages, run with -Verbose or $DebugPreference = 'Continue'
Test-OutputTypes -InputString "hello" -Verbose
Test-OutputTypes -InputString "debug" -Debug
Test-OutputTypes -InputString $null # This will generate an error
Demonstrating verbose, debug, and error output in a PowerShell function.