ps/Modules/Cole.PowerShell.Developer/Public/Get-MostUsedCommand.ps1
2023-05-30 22:51:22 -07:00

43 lines
1.2 KiB
PowerShell

function Get-MostUsedCommand {
<#
.SYNOPSIS
See your most used single-line commands.
.DESCRIPTION
Hobby projects are fun, yeah?
.PARAMETER Top
How many events do you want to see?
#>
[CmdletBinding(DefaultParameterSetName = 'Top')]
[OutputType([string[]])]
param(
[Parameter(ParameterSetName = 'Top')]
[int]$Top = 10,
[Parameter(ParameterSetName = 'MoreThan')]
[int]$MoreThan = 20,
[Parameter()]
[string[]]$AdditionalPath
)
$historyEntries = Get-HistoryEntries -SkipMultilines -AdditionalPath $AdditionalPath
$map = @{}
foreach ($entry in $historyEntries) {
$key = $entry.Trim()
if ($null -eq $map["$key"]) {
$map["$key"] = 0
}
$map["$key"] = $map["$key"] + 1
}
if ($PSCmdlet.ParameterSetName -eq 'Top') {
$show = $map.GetEnumerator() | Sort-Object -Property Value -Descending | Select-Object -First $Top
}
if ($PSCmdlet.ParameterSetName -eq 'MoreThan') {
$show = ($map.GetEnumerator() | Where-Object { $_.Value -gt $MoreThan }).GetEnumerator() | Sort-Object -Property Value -Descending
}
$show | Format-Table -Property Value, Name -Autosize
}