개요
Clear-Host는 현재 화면에 쌓인 명령·출력을 모두 지우고 프롬프트만 남기는 명령이다. CMD의 cls, Bash의 clear와 하는 일은 같지만, PowerShell에서는 이것이 컴파일된 cmdlet이 아니라 단순 함수(simple function)로 구현되어 있다는 점이 다르다. 이 챕터는 Clear-Host 자체와, 콘솔 창의 제목·색상처럼 화면을 지우는 것 이상으로 콘솔 자체를 다루는 설정을 함께 짚는다.
정신 모델은 “Clear-Host는 화면(디스플레이)만 지울 뿐 세션 상태는 건드리지 않는다"는 것이다. 화면을 지워도 정의해 둔 변수·함수·히스토리는 그대로 남아 있으며, 반대로 콘솔 창의 제목이나 색상처럼 화면 자체의 속성을 바꾸는 작업은 Clear-Host가 아니라 $Host.UI.RawUI 객체를 통해 이뤄진다.
사용법
| |
Clear-Host는 고급 함수가 아니라 단순 함수라 매개변수 자체가 없다. 호스트 프로그램(콘솔, Windows Terminal, VS Code 통합 터미널)에 따라 화면을 지우는 실제 구현이 다를 수 있어, “화면을 지운다"는 결과는 같아도 동작 방식은 호스트가 결정한다.
콘솔 창 제목·색상 설정
콘솔 창 자체의 속성은 $Host.UI.RawUI 객체의 속성을 직접 바꿔서 제어한다.
| 속성 | 설명 |
|---|---|
$Host.UI.RawUI.WindowTitle | 콘솔 창 제목 표시줄 텍스트 |
$Host.UI.RawUI.ForegroundColor | 기본 글자색 |
$Host.UI.RawUI.BackgroundColor | 기본 배경색 |
$Host.UI.RawUI.WindowSize | 콘솔 창 크기(가로×세로 문자 수) |
$PSStyle.Formatting.*(PowerShell 7.2+) | ANSI 색상 코드를 이용한 출력 스트림별 서식 |
| |
주의사항·함정
배경색을 바꾼 직후에는 Clear-Host가 필요하다: $Host.UI.RawUI.BackgroundColor를 바꿔도 이미 화면에 출력된 부분은 이전 배경색 그대로 남는다. 새 배경색을 화면 전체에 적용하려면 색상 변경 직후 Clear-Host를 호출해야 한다.
호스트마다 지원 범위가 다르다: $Host.UI.RawUI의 일부 속성(특히 WindowSize, BufferSize)은 실제 콘솔 창이 있는 호스트에서만 의미가 있다. VS Code 통합 터미널이나 원격 세션처럼 실제 콘솔 창이 없는 호스트에서는 이런 속성을 읽거나 쓸 때 오류가 날 수 있다.
이식성: CMD는 title·color 명령으로, Bash는 이스케이프 시퀀스(\e]0;제목\a)나 tput 명령으로 비슷한 설정을 하지만, PowerShell은 이 모든 것을 $Host.UI.RawUI라는 하나의 객체 속성 집합으로 통일해서 다룬다. 텍스트 이스케이프 코드를 외울 필요 없이 객체의 속성을 읽고 쓰는 것만으로 콘솔을 제어할 수 있다는 점이 객체 파이프라인의 장점이 콘솔 조작에도 적용된 사례다.
![Featured image of post [PowerShell] 08. Clear-Host — 화면 지우기와 콘솔 설정](/post/powershell/clear-host-command-console-title-color/wordcloud_hu_c514bb5f6d7ce51c.webp)
![[PowerShell] 06. $PROFILE — 프로파일 스크립트](/post/powershell/profile-variable-powershell-startup-script/wordcloud_hu_56ce94df4e3e6cb6.webp)
![[PowerShell] 07. PSReadLine — 탭 완성과 명령 히스토리](/post/powershell/psreadline-module-tab-completion-history/wordcloud_hu_a79d03efc2f079cc.webp)
![[PowerShell] 08. Clear-Host — 화면 지우기와 콘솔 설정](/post/powershell/clear-host-command-console-title-color/wordcloud_hu_5bf47c18c0df3890.webp)
![[PowerShell] 09. Format-Table/List/Wide — 출력 형식 제어](/post/powershell/format-table-list-wide-command-output/wordcloud_hu_551d06e00228d794.webp)
![[PowerShell] 10. 파이프라인 정신 모델 — 텍스트가 아닌 객체](/post/powershell/powershell-pipeline-object-model-not-text/wordcloud_hu_8f8cdb72019d8a67.webp)
![[PowerShell] 04. Get-Alias/Set-Alias/New-Alias — 별칭 시스템](/post/powershell/get-set-new-alias-command-powershell/wordcloud_hu_707c9d2c20ab9afd.webp)
![[PowerShell] 00. 과정 개요와 커리큘럼](/post/powershell/getting-started-powershell/wordcloud_hu_15979a96cd2a594f.webp)
![[PowerShell] 01. PowerShell 소개 — pwsh와 Windows PowerShell](/post/powershell/powershell-pwsh-introduction-cross-platform-shell/wordcloud_hu_a2e2aef4f86fd6a4.webp)
![[PowerShell] 02. Get-Help — 도움말 조회와 Update-Help](/post/powershell/get-help-command-powershell-help-system/wordcloud_hu_618b778795b5a246.webp)