Clear Host - PowerShell
Overview
The Clear-Host
command quickly and conveniently clears the current PowerShell console screen. This command is essential for maintaining a clean and organized work environment and is commonly used when working with large or complex outputs that can clutter the screen.
Syntax
Clear-Host [-Force]
Options/Flags
| Flag | Description | Default |
|—|—|—|
| -Force | Suppresses the confirmation prompt before clearing the screen. | False |
Examples
Simple Example:
Clear-Host
Example with Force Flag:
Clear-Host -Force
Common Issues
Confirmation Prompt: By default, Clear-Host
displays a confirmation prompt before clearing the screen. Use the -Force
flag to suppress this prompt.
Nested Sessions: Clear-Host
only clears the active console window. If PowerShell sessions are nested, it may be necessary to use the Clear-Host
command in each session.
Redistributing Output: To keep output from being redistributed to the host console, use the Out-Null
cmdlet after commands that generate output, as shown below:
Get-Process | Out-Null
Clear-Host
Integration
Clear-Host
can be integrated with other commands for advanced tasks. For example:
# Clear screen, get current date, and display
Clear-Host
Get-Date
Related Commands
cls
: A similar command available in thecmd
command prompt.Out-Null
: Redirects output to$Null
to prevent it from appearing on the console.