Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get your most common PowerShell commands by inspecting your PSReadLine history
$err=$null
[System.Management.Automation.PSParser]::Tokenize((Get-Content (Get-PSReadLineOption).HistorySavePath),[ref]$err) |
Where-Object {$_.type -eq 'command'} |
Select-Object Content | Group-Object Content |
Sort-Object Count, Name -Descending | Select-Object Count, Name -First 20
@vexx32

This comment has been minimized.

Copy link

commented Jun 14, 2019

PSParser is ancient and doesn't handle things well; use System.Management.Automation.Parser.ParseInput() instead:

$Errors = $null
$Tokens = $null
$Ast = [System.Management.Automation.Language.Parser]::ParseInput(
    ( Get-Content (Get-PSReadLineOption).HistorySavePath ),
    [ref]$Tokens,
    [ref]$Errors
)
$Tokens | 
    Where-Object { $_.TokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::CommandName) } |
    Select-Object Text | Group-Object Text |
    Sort-Object Count, Name -Descending | Select-Object Count, Name -First 20
@KirkMunro

This comment has been minimized.

Copy link

commented Jun 14, 2019

@vexx32 Your script gives me completely different (inaccurate) results. The one at the top works properly.

@vexx32

This comment has been minimized.

Copy link

commented Jun 14, 2019

Yeah, it does. Interesting.

Problem for another day! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.