当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Clear-History (Microsoft.PowerShell.Core)

作者:精品下载站 日期:2024-12-14 02:17:08 浏览:13 分类:玩电脑

Clear-History (Microsoft.PowerShell.Core)


Clear-History

模块 :Microsoft.PowerShell.Core

从 PowerShell 会话命令历史记录中删除条目。

句法

Clear-History
     [[-Id] <int[]>]
     [[-Count] <int>]
     [-Newest]
     [-WhatIf]
     [-Confirm]
     [<CommonParameters>]
Clear-History
     [[-Count] <int>]
     [-CommandLine <string[]>]
     [-Newest]
     [-WhatIf]
     [-Confirm]
     [<CommonParameters>]

描述

Clear-History 从 PowerShell 会话中删除命令历史记录。每个 PowerShell 会话都有自己的命令历史记录。要显示命令历史记录,请使用 Get-History cmdlet。

默认情况下,Clear-History 会从 PowerShell 会话中删除整个命令历史记录。您可以将参数与 Clear-History 一起使用来删除选定的命令。

Clear-History 不会清除 PSReadLine 命令历史记录文件。 PSReadLine 模块存储一个历史文件,其中包含每个 PowerShell 会话中的每个 PowerShell 命令。在 PowerShell 提示符下,使用键盘上的向上和向下箭头滚动命令历史记录。要显示命令历史记录的 PSReadLine 配置,请使用 Get-PSReadLineOptionPSReadLine 随 PowerShell 5.0 及更高版本一起提供。有关详细信息,请参阅 about_PSReadLine。

示例

示例 1:从 PowerShell 会话中删除命令历史记录

此命令将从 PowerShell 会话历史记录中删除所有命令。

Get-History

Id CommandLine
  -- -----------
   1 Set-Location .\Test
   2 Update-Help
   3 Set-Location C:\Test\Logs
   4 Get-Location

Clear-History
Get-History

Id CommandLine
  -- -----------
   5 Clear-History

Get-History cmdlet 显示 PowerShell 会话的历史记录。 Clear-History 删除整个命令历史记录。 Get-History 显示更新的命令历史记录并确认之前的历史记录已被删除。

示例2:删除最新命令

此命令使用 CountNewest 参数从 PowerShell 会话历史记录中删除最新命令。

Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-Command Clear-History
   3 Get-Command Clear-History -Syntax
   4 Get-Command Clear-History -ShowCommandInfo
   5 Get-Help Get-Alias
   6 Get-Command Get-ChildItem -Syntax
   7 Get-Help Clear-History
   8 Set-Location C:\Test\Logs
   9 Get-Help Get-Variable
  10 Get-Help Get-ChildItem

Clear-History -Count 5 -Newest
Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-Command Clear-History
   3 Get-Command Clear-History -Syntax
   4 Get-Command Clear-History -ShowCommandInfo
   5 Get-Help Get-Alias
  11 Clear-History -Count 5 -Newest

Get-History cmdlet 显示 PowerShell 会话的历史记录。 Clear-History 用于删除命令历史记录。 Count 参数指定要删除的命令数,包括指定的IdNewest 参数指定从历史记录中清除最新命令。 Get-History 显示更新的命令历史记录并确认已删除五个最新命令,Id 6 - Id 10

示例 3:删除符合特定条件的命令

此命令删除与 CommandLine 参数定义的特定条件匹配的命令。

Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-Command Clear-History
   3 Get-Command Clear-History -Syntax
   4 Get-Command Clear-History -ShowCommandInfo
   5 Get-Help Get-Alias
   6 Get-Command Get-ChildItem -Syntax
   7 Get-Help Clear-History

Clear-History -CommandLine *Help*, *Syntax
Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-Command Clear-History
   4 Get-Command Clear-History -ShowCommandInfo
   8 Clear-History -CommandLine *Help*, *Syntax

Get-History cmdlet 显示 PowerShell 会话的历史记录。 Clear-History 删除命令历史记录。 CommandLine 参数指定包含帮助或以语法结尾的命令。 Get-History 显示更新的命令历史记录并确认命令 Id 3Id 5Id 6ID 7 已删除。

示例4:通过Id号删除命令

此命令使用Id删除特定历史记录项目。要删除多个命令,请提交以逗号分隔的 Id 数字列表。

Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-History
   3 Get-Help Get-Alias
   4 Get-Command Clear-History
   5 Get-Command Clear-History -Syntax
   6 Get-Command Clear-History -ShowCommandInfo

Clear-History -Id 3, 5
Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-History
   4 Get-Command Clear-History
   6 Get-Command Clear-History -ShowCommandInfo
   7 Get-History
   8 Clear-History -Id 3, 5

Get-History cmdlet 显示 PowerShell 会话的历史记录。 Clear-History 删除命令历史记录。 Id 参数指定要删除的命令。 Get-History 显示更新的命令历史记录并确认 Id 3Id 5 已删除。

示例 5:按 ID 号和计数删除命令

此命令使用 IdCount 参数来删除命令历史记录。命令按照相反的顺序从指定的Id 中删除,从最新到最旧。

Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-Command Clear-History
   3 Get-Command Clear-History -Syntax
   4 Get-Command Clear-History -ShowCommandInfo
   5 Get-Help Get-Alias
   6 Get-Command Get-ChildItem -Syntax
   7 Get-Help Clear-History
   8 Set-Location C:\Test\Logs
   9 Get-Help Get-Variable
  10 Get-Help Get-ChildItem

Clear-History -Id 7 -Count 5
Get-History

Id CommandLine
  -- -----------
   1 Set-Location C:\Test\
   2 Get-Command Clear-History
   8 Set-Location C:\Test\Logs
   9 Get-Help Get-Variable
  10 Get-Help Get-ChildItem
  11 Clear-History -Id 7 -Count 5

Get-History cmdlet 显示 PowerShell 会话的历史记录。 Clear-History 删除命令历史记录。 Id 参数指定以 Id 7 开头。 Count 参数指定删除五个命令,包括指定的IdGet-History 显示更新的命令历史记录并确认删除了五个命令,Id 3 - Id 7

参数

-CommandLine

从 PowerShell 会话中删除命令历史记录。该字符串必须完全匹配,或者使用通配符来匹配 Get-History 显示的 PowerShell 会话历史记录中的命令。如果您输入多个字符串,Clear-History 会删除与任何字符串匹配的命令。 CommandLine 参数可与Count 一起使用。

对于带有空格的字符串,请使用单引号。有关更多信息,请参阅 about_Quoting_Rules。

类型 :

String[]

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

True

-Confirm

在运行 Clear-History cmdlet 之前提示您进行确认。

类型 :

SwitchParameter

别名:

cf

位置:

命名

默认值:

False

必需的:

False

接受管道输入:

False

接受通配符:

False

-Count

指定Clear-History 删除的历史记录条目数。命令按顺序删除,从历史记录中最旧的条目开始。

CountId 参数可以一起使用。 Count 参数指定要删除的命令数,包括指定的Id。从指定的 Id 开始,按相反的顺序删除命令。例如,如果 Id 为 30,Count 为 10,则 Clear-History 将删除第 21 至 30 项。

CountCommandLine 参数可以一起使用。 Count 指定要删除与 CommandLine 参数值匹配的命令的数量。命令按顺序删除。

类型 :

整数32

位置:

1

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

-Id

指定Clear-History 删除的命令历史记录Id。要显示 Id 编号,请使用 Get-History cmdlet。 Id 数字是连续的,并且命令在整个 PowerShell 会话中保留其 Id 数字。 Id 参数可与CountNewest 一起使用。

类型 :

Int32[]

位置:

0

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

-Newest

当使用 Newest 参数时,Clear-History 会删除历史记录中的最新条目。默认情况下,Clear-History 会删除历史记录中最旧的条目。

Newest 参数可与 IdCount 一起使用。 Count 参数指定要删除的命令数,包括指定的Id。从指定的Id开始,命令将按顺序删除。例如,如果 Id 为 30,Count 为 10,则 Clear-History 将删除第 30 到 39 项。

类型 :

SwitchParameter

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

-WhatIf

显示运行 Clear-History cmdlet 时会发生什么情况。该 cmdlet 未运行。

类型 :

SwitchParameter

别名:

wi

位置:

命名

默认值:

False

必需的:

False

接受管道输入:

False

接受通配符:

False

输入

您无法通过管道将对象传递到此 cmdlet。

输出

此 cmdlet 不返回任何输出。

笔记

PowerShell 包含以下 Clear-History 别名:

  • 所有平台:

      clhy

    PowerShell 会话历史记录是 PowerShell 会话期间输入的命令的列表。您可以查看历史记录、添加和删除命令以及运行历史记录中的命令。有关更多信息,请参阅 about_History。

    会话历史记录与 PSReadLine 模块维护的历史记录分开管理。两个历史记录都可以在加载 PSReadLine 的会话中使用。此 cmdlet 仅适用于会话历史记录。有关详细信息,请参阅 about_PSReadLine。

    您需要 登录账户 后才能发表评论

    取消回复欢迎 发表评论:

    关灯