Friday, August 10, 2018

Module for searching your full PowerShell command history

By default the Get-History cmdlet only returns lines from your current session. Therefore I've created a small module that gets history items from your full history, by default the most recent 4096 commands.
It features an easy search option and a few switches for case sensitivit and uniqueness.

To have it automatically available every time you start PowerShell do the following.

1. Find a module path by running
$env:PSModulePath
2. Select one. I picked "C:\Users\jgste\OneDrive\Documenten\WindowsPowerShell\Modules"
3. Ensure this folder exists, else create it
4. Create a subfolder "FullHistory"
5. Save the code below as "FullHistory.psm1" in the new folder
6. Start a new PowerShell session. Get-FullHistory should be available.


Function Get-FullHistory {

<#
.Synopsis
Get lines from the full history of PowerShell commands
.Description
This command will query your PowerShell command log. By default this keeps your most recent 4096 commands
.Parameter FilterValue
A string to filter your results by.
.Parameter CaseSensitive
Set to filter Case Sensitive. Default is insensitive
.Parameter Unique
Set to false to receive duplicate results. Default is true
.Parameter HistoryPath
Set to use an alternative path to the history file. Usefull when you want use another users history.

.Example
PS C:\> Get-FullHistory content
get-command Get-PnPContentType
Get-PnPProvisioningTemplate -Out ".\MyFields.pnp"  -Handlers Fields,ContentTypes
get-content .\newfile.txt
get-content .\bubblesort.ps1
        
.Example
PS C:\> Get-FullHistory content -Unique:$false
get-command Get-PnPContentType
Get-PnPProvisioningTemplate -Out ".\MyFields.pnp"  -Handlers Fields,ContentTypes
get-command Get-PnPContentType
get-content .\newfile.txt
get-content .\newfile.txt
get-content .\bubblesort.ps1

.Example
PS C:\> Get-FullHistory content -CaseSensitive
get-content .\newfile.txt
get-content .\bubblesort.ps1

.Link
https://sharedpointers.blogspot.com
#>
    [cmdletbinding()]
    Param(
        [Parameter(Position=0)]        
        [String]$FilterValue,
        [Switch]$CaseSensitive = $false,
        [Switch]$Unique = $true,
        [String]$HistoryPath = $HOME + "\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt"
    )
 
    Begin {
        Write-Verbose "Starting $($MyInvocation.Mycommand)"
        if (-not (Test-Path $HistoryPath)) {
            Write-Host "Could not find history file at $HistoryPath !"
            exit
        } 
        $FilterValue = "*" + $FilterValue + "*"
    } 
 
    Process { 
        [System.IO.File]::ReadLines($HistoryPath)| Foreach-Object { 
            if ($FilterValue) {
                if ($CaseSensitive) {
                    if ($_ -clike $FilterValue){
                        return $_
                    }
                }
                else {
                    if ($_ -like $FilterValue) {
                        return $_
                    }
                }
            }
            else {
                return $_
            }
        } | Select-Object -Unique:$Unique 
    } 
 
    End {
        Write-Verbose "Ending $($MyInvocation.Mycommand)"
    } 
}


Rating