-
Notifications
You must be signed in to change notification settings - Fork 29
/
Remove-ConsoleHistory.ps1
81 lines (72 loc) · 2.33 KB
/
Remove-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
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
.SYNOPSIS
Removes an entry from the DOSKey-style console command history (up arrow or F8).
.INPUTS
System.String containing exact commands to remove.
.FUNCTIONALITY
Console
.EXAMPLE
Remove-ConsoleHistory.ps1 -Id 42
Deletes the 42nd command from the history.
.EXAMPLE
Remove-ConsoleHistory.ps1 -Duplicates
Deletes any repeated commands from the history.
.EXAMPLE
Remove-ConsoleHistory.ps1 -Like winget*
Deletes any commands that start with "winget" from the history.
#>
#Requires -Version 7
[CmdletBinding(ConfirmImpact='High',SupportsShouldProcess=$true)] Param(
[Parameter(ParameterSetName='Id',Mandatory=$true)][int] $Id,
[Parameter(ParameterSetName='CommandLine',Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $CommandLine,
[Parameter(ParameterSetName='Like',Mandatory=$true)][string] $Like,
[Parameter(ParameterSetName='Duplicates',Mandatory=$true)][switch] $Duplicates
)
Begin
{
$history = Join-Path $env:AppData Microsoft Windows PowerShell PSReadline ConsoleHost_history.txt
}
Process
{
switch($PSCmdlet.ParameterSetName)
{
Id
{
$cmd = Get-Content $history -TotalCount $Id |Select-Object -Last 1
if($PSCmdlet.ShouldProcess($cmd, 'remove'))
{
$i = 1
(Get-Content $history) |Where-Object {$i++ -ne $Id} |Out-File $history -Encoding utf8NoBOM
Write-Verbose "Removed '$cmd'"
}
}
CommandLine
{
[bool] $found = Get-Content $history |Where-Object {$_ -eq $CommandLine} |Select-Object -First 1
if($found -and $PSCmdlet.ShouldProcess($CommandLine, 'remove'))
{
(Get-Content $history) -ne $CommandLine |Out-File $history -Encoding utf8NoBOM
if($found) {Write-Verbose "Removed '$CommandLine'"}
}
}
Like
{
$cmd = (Get-Content $history) -like $Like
if($PSCmdlet.ShouldProcess("$($cmd.Count) commands matching '$Like'", 'remove'))
{
(Get-Content $history) -notlike $Like |Out-File $history -Encoding utf8NoBOM
$cmd |ForEach-Object {Write-Verbose "Removed '$_'"}
}
}
Duplicates
{
if($PSCmdlet.ShouldProcess('duplicate commands', 'remove'))
{
$before = (Get-Content $history).Count
(Get-Content $history) |Select-Object -Unique |Out-File $history -Encoding utf8NoBOM
$after = (Get-Content $history).Count
Write-Verbose "Removed $($before - $after) duplicate entries"
}
}
}
}