Skip to content

Instantly share code, notes, and snippets.

@devblackops
Created June 13, 2019 23:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devblackops/9cbfb88d426426e6a45cab724a9a2a27 to your computer and use it in GitHub Desktop.
Save devblackops/9cbfb88d426426e6a45cab724a9a2a27 to your computer and use it in GitHub Desktop.
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
Copy link

vexx32 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
Copy link

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

@vexx32
Copy link

vexx32 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