我们通常会在Console界面中运行多次命令或者命令行,很多人会使用键盘的上下键来翻动之前使用的命令,但是当我们敲过运行的命令次数越多那么即便你去使用上下键去翻页查找之前使用的命令也依然会显得不是那么方便。
其实,在PowerShell中我们可以使用管理历史记录的命令来管理那些之前使用过的命令行,目前在PowerShell中有如下四个管理历史相关的命令。
Cmdlet (Alias) Description
------------------- ------------------------------------------
Get-History (h) Gets the command history.
Invoke-History (r) Runs a command in the command history.
Add-History Adds a command to the command history.
Clear-History (clh) Deletes commands from the command history.
C:\Users\Administrator> Get-History
Id CommandLine
-- -----------
1 registry
2 cls
3 $PSDefaultParameterValues = @{"Get-Process:Name" = "notepad"}
4 gps
5 gps -Verbose
6 $PSDefaultParameterValues = @{"Get-Process:Name" = "notepad"}
7 Get-Process
8 $PSDefaultParameterValues = @{"Get-Process:Name" = {(Get-Process -Id 1204)|select processname}}
PS C:\Users\Administrator> $MaximumHistoryCount
4096
你也可以直接给这个变量赋一个阿拉伯数字设置你想设置的上限值,比如我设置为5:
PS C:\Users\Administrator> $MaximumHistoryCount = 5
PS C:\Users\Administrator> $MaximumHistoryCount
5
PS C:\Users\Administrator> Get-History
Id CommandLine
-- -----------
46 $MaximumAliasCount
47 $MaximumHistoryCount = 200
48 $MaximumHistoryCount
49 $MaximumHistoryCount = 5
50 $MaximumHistoryCount
PS C:\Users\Administrator> Invoke-History -id 51
Get-History
Id CommandLine
-- -----------
51 Get-History
52 Invoke-History -id 46
53 Invoke-History -Id 46
54 Invoke-History -Id 46
55 Get-History