-
Notifications
You must be signed in to change notification settings - Fork 29
/
Get-ConsoleHistory.ps1
68 lines (60 loc) · 1.58 KB
/
Get-ConsoleHistory.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
.SYNOPSIS
Returns the DOSKey-style console command history (up arrow or F8).
.OUTPUTS
System.Management.Automation.PSObject with these properties:
* Id: The position of the command in the console history.
* CommandLine: The command entered in the history.
.FUNCTIONALITY
Console
.EXAMPLE
Get-ConsoleHistory.ps1 -Like *readme*
Id CommandLine
-- -----------
30 gc .\README.md
56 gc .\README.md
.EXAMPLE
Get-ConsoleHistory.ps1 -Id 30
Id CommandLine
-- -----------
30 gc .\README.md
#>
#Requires -Version 3
[CmdletBinding()] Param(
[Parameter(ParameterSetName='Id',Mandatory=$true)][int] $Id,
[Parameter(ParameterSetName='Like',Mandatory=$true)][string] $Like,
[Parameter(ParameterSetName='Match',Mandatory=$true)][string] $Match,
[Parameter(ParameterSetName='All')][switch] $All
)
$history = Join-Path $env:AppData Microsoft Windows PowerShell PSReadline ConsoleHost_history.txt
switch($PSCmdlet.ParameterSetName)
{
Id
{
[pscustomobject]@{
Id = $Id
CommandLine = Get-Content $history -TotalCount $Id |Select-Object -Last 1
}
}
Like
{
$id = 0
(Get-Content $history) |Where-Object {$id++; $_ -like $Like} |ForEach-Object {[pscustomobject]@{
Id = $id
CommandLine = $_
}}
}
Match
{
Select-String -Pattern $Match -Path $history |ForEach-Object {[pscustomobject]@{
Id = $_.LineNumber
CommandLine = $_.Line
}}
}
default
{
$id = 0
Get-Content (Join-Path $env:AppData Microsoft Windows PowerShell PSReadline ConsoleHost_history.txt) |
ForEach-Object {[pscustomobject]@{Id=++$id;CommandLine=$_}}
}
}