Windows PowerShell具有内置的历史记录(History)功能,可以记住您在使用它时执行的所有命令。虽然它应该记住活动会话的历史记录(History),但我看到它保留的不止这些。在这篇文章中,我将展示如何查看PowerShell命令历史(History)并巧妙地使用它。
如何查看PowerShell 命令历史记录(PowerShell Command History)
具有历史功能的最显着优势是您不必再次键入来执行您已经执行过一次的特定命令。但是,通过数百个命令进行递归并没有多大帮助。搜索它们或运行您只记得部分的特定命令怎么样?遵循这些方法。
- 键盘法
- (Search Forward)在PowerShell 历史记录中(PowerShell History)向前或向后搜索(Backward)
- 查看(View)此会话的整个历史记录
- (Invoke)从PowerShell(PowerShell)命令历史中(History)调用特定命令
- 在历史中搜索
最后,我们还解释了如何清除、导出和导入PowerShell 命令的 历史记录(History)。如果您需要经常执行相同的命令,非常方便。
1]键盘方法
如果您使用Windows一段时间,我相信您已经使用过一次命令提示符。(Command Prompt)如果您使用上下箭头键,您可以来回执行命令。这同样适用于PowerShell。但这里是转折点。如果您只记得部分命令,这里是如何搜索。
- 键入(Type)您记得的部分命令
- 按 F8(Press F8),并按住它直到找到。
- 搜索词将以荧光绿色突出显示,然后是命令的其余部分。
唯一的缺点是搜索词必须是命令的开头。
2]在历史中(History)向前(Search Forward)或向后搜索(Backward)
如果要在History中向前或向后搜索,则可以使用Ctrl + R和Ctrl + S.前者允许您在History中向后搜索,而后者则向前。使用键盘快捷键后,您应该会在底部获得一个片段(bck-i-search 或 fwd-i-search)。键入您要搜索的内容,即使文本属于命令中间的某个位置,它也会搜索它。与我们上面使用的 F8 方法相比,它要好得多。
3]查看(View)此会话的整个PowerShell命令历史(History)
键入 History(Type History),然后按Enter键。您应该能够看到您在当前会话中执行的所有命令。虽然您可以使用箭头键查看在早期会话中执行的命令,但历史记录(History)中没有它。如果您按“H”键(key “H”)并按回车键,它会显示命令历史记录(History)。将其视为 历史(History)命令的快捷方式。
4]从历史中调用(Invoke)特定命令
如果您注意到,上面的图像在每个命令旁边都有一个 ID。如果要执行列表中的一个特定命令,则需要使用Invoke-History。
- 记下要执行的命令的 ID
- 键入Invoke-History <ID>. 将 <ID> 替换为确切的数字
- 按(Press)Enter 键(Enter Key),它将执行History中的命令。
5]在历史中搜索
在History(History) of commands 中搜索的最简单方法是在History Output上使用(History Output)Select-String方法。
- 键入以下内容,并将Get替换为您要搜索的术语
- 获取历史 | 选择字符串模式“获取”。
- 它将显示与搜索命令匹配的所有命令。
由于您获得了 id,因此您可以使用Invoke History命令立即执行它。
清除、导出(Export)和导入 PowerShell 历史记录(Import PowerShell History)
要从History中删除所有命令,您只需执行“ Clear-History ”命令即可。请注意,即使在这样做之后,您仍然可以使用向上和向下箭头键访问该命令。
要导出所有命令(export all the commands),您可以使用 Export-Clixml 或 Export-CSV( Export-Clixml or Export-CSV)格式。这是示例命令
Get-History | Export-Clixml -Path <Path>\PSHistorycommands.xml
Get-History | Export-Csv -Path <Path>\PSHistorycommands.csv
要将这些命令导入回另一个或新会话( import these commands back to another or new session),请使用以下命令。
Add-History -InputObject (Import-Clixml -Path <Path>\PSHistorycommands.xml)
Add-History -InputObject (Import-Csv -Path <Path>\PSHistorycommands.csv)
您可以随时打开此CSV或XML文件以立即查看PowerShell命令历史记录(History)。这几乎总结了PowerShell 命令历史(PowerShell Command History)的几乎所有内容,以及如何使用 cumbers 或通过搜索它们或在第二天将它们导入回来重用它们。
How to see PowerShell command History on Windows 10
Windows PowerShell has a built-in History feature that remembers all the commands you executed when using it. While it should remember the History of the active session, I see that it retains more than that. In this post, I will show how you can see PowerShell command History and use it smartly.
How to see PowerShell Command History
The most significant advantage of having a History feature is you don’t have to type again to execute a particular command you already executed once. However, recursing through hundreds of commands is not much of help. How about searching through them or run a specific command you only remember partially? Follow these methods.
- Keyboard Method
- Search Forward or Backward in PowerShell History
- View the entire History of this session
- Invoke a particular command from PowerShell command History
- Search within History
In the end, we have also explained how you can clear, export, and import PowerShell command’s History. Very handy if you need to execute the same commands often.
1] Keyboard Method
I am sure you have used the Command Prompt once if you have used Windows for some time. If you use the arrow keys up and down, you can go back and forth of the executed commands. The same applies to PowerShell as well. But here is the twist. If you remember a command partially, here is how to search.
- Type part of the command you remember
- Press F8, and keep pressing it until you find.
- The search term will be highlighted with a fluorescent green, followed by the rest of the command.
The only drawback is that the search term has to be the start of the command.
2] Search Forward or Backward in History
If you want to search forward or backward in History, then you can use Ctrl + R and Ctrl + S. The former lets you search back in History while the later forward. As soon as you use the keyboard shortcut, you should get a snippet at the bottom (bck-i-search or fwd-i-search). Type what you want to search, and even if the text belongs to somewhere in the middle of the command, it will search for it. It is much better compared to the F8 method we used above.
3] View the entire PowerShell command History of this session
Type History, and press the Enter key. You should be able to see all the commands you executed in the current session. While you can view the commands executed in the earlier session using the arrow keys, the History will not have it. If you press the key “H” and hit enter, it will do reveal the History of commands. Consider it as a shortcut to the History command.
4] Invoke a particular command from History
If you notice, the image above has an ID next to each command. If you want to execute one specific command from the list, you need to use Invoke-History.
- Note the ID of the command you want to execute
- Type Invoke-History <ID>. Replace <ID> with the exact number
- Press the Enter Key, and it will execute that command from History.
5] Search within History
The easiest way to search within History of commands is to use the Select-String method on History Output.
- Type the following, and replace Get by the term you want to search
- Get-History | Select-String -Pattern “Get.”
- It will reveal all the commands which match the search commands.
Since you get the id along, you can use Invoke History command to execute it instantly.
Clear, Export, and Import PowerShell History
To delete all the commands from History, all you need to do is execute the command “Clear-History.” Do note that even after doing this, you can still access the command using up and down arrow keys.
To export all the commands, you can use the Export-Clixml or Export-CSV format. Here is the sample command
Get-History | Export-Clixml -Path <Path>\PSHistorycommands.xml
Get-History | Export-Csv -Path <Path>\PSHistorycommands.csv
To import these commands back to another or new session, use the following command.
Add-History -InputObject (Import-Clixml -Path <Path>\PSHistorycommands.xml)
Add-History -InputObject (Import-Csv -Path <Path>\PSHistorycommands.csv)
You can open this CSV or XML file anytime to see PowerShell command History instantly. This pretty much sums up almost everything around PowerShell Command History, and how you can reuse them using cumbers or by searching them or importing them back the next day.