Out Host - PowerShell
Overview
The Out-Host
command displays output to the console window of the current PowerShell session. It is primarily used for providing informational messages, debugging output, or any other text-based output to the user.
Syntax
Out-Host [-ErrorAction <Action>] [-ForegroundColor <Color>] [-BackgroundColor <Color>] [-InputObject <Object>] [-OutBuffer <bool>] [-Width <int>] [-Append] [-NoNewline]
Options/Flags
- -ErrorAction: Specifies the action to be taken when an error occurs. Valid values include:
Stop
,SilentlyContinue
,Continue
,Suspend
, andIgnore
. Default:Continue
. - -ForegroundColor: Sets the foreground color of the output text. Valid values are the standard color names or the escape sequences below:
- Red:
[Esc][91m
, Green:[Esc][92m
, Yellow:[Esc][93m
- Blue:
[Esc][94m
, Magenta:[Esc][95m
, Cyan:[Esc][96m
- White:
[Esc][97m
- Default:
White
- Red:
- -BackgroundColor: Sets the background color of the output text using the same escape sequences as
ForegroundColor
. Default:Black
- -InputObject: Specifies the input object to be displayed. If not provided, no input will be displayed.
- -OutBuffer: Specifies whether to buffer the output before displaying it. If
$true
, the output will be collected in a buffer before being displayed. Default:$false
- -Width: Sets the maximum width of the displayed output. Lines longer than the specified width will be wrapped. Default:
System Console Width
- -Append: Appends the output to the current line instead of creating a new line.
- -NoNewline: Prevents a newline from being added after the output.
Examples
# Display a simple message
Out-Host "Hello, PowerShell!"
# Display a warning with a red foreground
Out-Host -ForegroundColor Red "Warning: This is important!"
# Display a progress bar with a blue background
Out-Host -BackgroundColor Blue "Progress: [=========]"
# Display the output of Get-Command
Out-Host (Get-Command)
# Display custom objects with -InputObject
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name Name -Value "John Smith"
$obj | Add-Member -MemberType NoteProperty -Name Email -Value "john.smith@example.com"
Out-Host -InputObject $obj
Common Issues
- Output not colored: Ensure the PowerShell console supports ANSI escape sequences.
- Output not wrapped: Set the appropriate value for
-Width
. - Output displayed in separate lines: Use
-Append
to prevent a newline. - ANSI escape sequences not recognized: Use the
-ForegroundColor
and-BackgroundColor
parameters instead of escape sequences.
Integration
Out-Host
can be combined with other commands to create informative scripts and workflows. For example, you can use it to:
- Display error messages from
Try-Catch
blocks - Create custom progress bars
- Provide feedback to users during script execution
Related Commands
Write-Host
Format-Host
Write-Error