如何在 PowerShell 中清除屏幕

通常,PowerShell 用户可能会经历控制台的混乱界面,因为多个命令确实有各种输出。为了摆脱这种情况,我们需要清除 PowerShell 控制台的屏幕。
本文提供了几种清除 PowerShell 屏幕脚本环境的方法。
PowerShell 清屏介绍
PowerShell 清屏方法由 Clear-Host
函数及其两个别名 cls
和 clear
支持。本节的计划旨在指导您如何清除 PowerShell 屏幕。
Clear-Host
在 PowerShell 中使用 Clear-Host
命令
PowerShell 自动化依赖于其本机函数、cmdlet 和 PowerShell 支持的别名。例如,Windows PowerShell 的 Clean-Host
命令清除控制台的屏幕。
这是 PowerShell 的一个独立操作,不能被管道或与任何其他 cmdlet 一起使用。它不会生成输出。
我们可以通过以下方式应用 Clear-Host
函数。
PowerShell 中 Clear-Host
命令的别名
PowerShell 中的别名与特定的本机命令相关联。然而,我们可以自定义并为 PowerShell 中的任何函数或 cmdlet 创建别名。
例如,Clear-Host
函数支持两个内置别名 cls
和 clear
。
cls
此外,可以通过以下方式使用 clear
别名清除 PowerShell 的控制台。
clear
我们可以通过 Get-Alias
命令验证这些别名使用 Clear-Host
函数。
示例代码:
Get-Alias clear
Get-Alias cls
输出:
PS C:\temp> Get-Alias Clear
CommandType Name Version Source
----------- ---- ------- ------
Alias clear -> Clear-Host
PS C:\temp> Get-Alias Cls
CommandType Name Version Source
----------- ---- ------- ------
Alias cls -> Clear-Host
我们已经讨论了在上面的部分中清除 PowerShell 屏幕的方法。然而,观察到 PowerShell 命令对大小写不敏感,这个功能也适用。
因此,我们可以得出结论,我们可以使用 Clear-Host
及其别名,不管字母大小写(大写或小写)。
结论
PowerShell 是一个创新的命令行工具,支持所有操作系统的跨平台功能。此外,该脚本语言有许多函数和 cmdlet 支持别名以执行多个任务。
在本文中,我们介绍了如何清除 PowerShell 屏幕。
第一种方法使用 Clear-Host
函数来获得更清洁的脚本控制台。其他两种方法 Cls
和 Clear
也与 Clear-Host
相关,因为它们是 Clear-Host
的别名。
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn