如何在Windows Server 2016中的所有PowerShell会话中查看命令历史记录?

Dan*_*ach 22 powershell windows-server-2016

在哪里可以查看Windows Server 2016中所有会话的完整历史记录?

以下PowerShell命令仅包含当前会话中的命令:

Get-History
Run Code Online (Sandbox Code Playgroud)

Dan*_*ach 55

在PowerShell中输入以下命令:

(Get-PSReadlineOption).HistorySavePath
Run Code Online (Sandbox Code Playgroud)

这为您提供了保存所有历史记录的路径.然后在文本编辑器中打开路径.

尝试cat (Get-PSReadlineOption).HistorySavePath在PowerShell中列出历史记录.

  • 为什么这不是历史命令的默认行为? (7认同)
  • 基本上只使用 `cat (Get-PSReadlineOption).HistorySavePath` 来获取整个历史记录。谢谢@DanielLeach (3认同)
  • 使用 `cat (Get-PSReadlineOption).HistorySavePath | 选择 -Last 10` 获取最后 10 行。如果您的历史记录中有很多命令,则很有用。 (3认同)
  • 实际上,它在全新安装的 Windows Server 2016 中默认存在。 (2认同)
  • 它出现在我的Windows 10 Pro(1703)中.但我不知道默认情况下它是否存在,或者Visual Studio或其他类似工具是否安装了某些模块.在我的lates bluescreen之后,它帮助了我. (2认同)
  • 它默认存在于Windows 10 Enterprise 64位上. (2认同)

小智 24

为了从 PowerShell 获取完整历史记录并将输出保存到文件中,我使用以下命令:

Get-Content (Get-PSReadlineOption).HistorySavePath > D:\PowerShellHistory.txt
Run Code Online (Sandbox Code Playgroud)

  • 获取历史文件的内容并将其转储到另一个文件似乎有点多余。您只需执行“copy (Get-PSReadlineOption).HistorySavePath d:\PowerShellHistory.txt”即可 (5认同)

Cod*_*jay 15

在 Windows PowerShell 上

要进入会话,您可以使用 hhistory

但要获取您使用的计算机中编写的所有命令

cat (Get-PSReadlineOption).HistorySavePath


小智 11

由于您使用的是 Windows,因此您还可以使用下面的命令来打开“记事本”。

notepad (Get-PSReadlineOption).HistorySavePath
Run Code Online (Sandbox Code Playgroud)


san*_*boy 6

其中提到了 Windows Server/Enterprise 版本,但作为 Pro(标准零售版本)用户,HistorySavePath我也可以使用。我需要查看最近在较旧的会话中安装了哪些 python 软件包,并想在此处为在历史中寻找特定内容的人们添加答案。

# if you like file names and line numbers included in your output
Select-String "<search pattern>" (Get-PSReadlineOption).HistorySavePath

# if you Just want the text without any of the other information
Get-Content (Get-PSReadlineOption).HistorySavePath | Select-String "<search pattern>" 
Run Code Online (Sandbox Code Playgroud)

就我而言,我跑了

Select-String 'pip install' (Get-PSReadlineOption).HistorySavePath
Run Code Online (Sandbox Code Playgroud)

这给了我一个从我之前的会话中运行的 pip install 命令列表

...
[Path/To/File]:10401:pip install dash
[Path/To/File]:10824:pip install -r .\requirements.txt
[Path/To/File]:11296:pip install flask-mysqldb
[Path/To/File]:11480:pip install Flask-Markdown
[Path/To/File]:11486:pip install pygments
[Path/To/File]:11487:pip install py-gfm
[Path/To/File]:11540:pip install bs4
Run Code Online (Sandbox Code Playgroud)