PowerShell 技能连载 - 利用命令行历史

PowerShell 将您的所有交互命令行输入“记录”到它的命令行历史中,而 Get-History 负责显示它们。如果您运行了一段时间 PowerShell 并且觉得运行的效果不错,那么可以用以下脚本将所有的交互命令从命令行历史中复制到剪贴板中。接下来您可以将它们粘贴到 PowerShell ISE 中,并使之成为一个脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# define how old your commands may be at most to be included
$MaxAgeHours = 4

# get all command history items that were started after this
$DateLimit = (Get-Date).AddHours(-$MaxAgeHours)


# get all command-line commands
Get-History |
# exclude all that were aborted
Where-Object ExecutionStatus -eq Completed |
# exclude all that are older than the limit set above
Where-Object StartExecutionTime -gt $DateLimit |
# get just the command-line
Select-Object -ExpandProperty CommandLine |
# copy all to clipboard
clip.exe

本文国际来源:Exploiting Your Command History

评论