Powershell Get-History and Invoke-History

I use Get-History (‘ghy’) and Invoke-History (‘ihy’) frequently, so I started thinking about streamlining their use.

I came up with Run-CmdFromHistory:
function Run-CmdFromHistory {
     param (
        [Parameter(Mandatory=$false)]
        [Int32] $cmdCnt = 15
    )
    $cmds=get-history | select -last $cmdCnt;
    $cmds;

    Write-Host “Enter a cmd id to run (empty to cancel)” -Foreground Yellow;
    $id = Read-Host ” “;
    if([string]::IsNullOrEmpty($id) -eq $false) {
        invoke-history $id;
    }
    else {
        “Canceled.”;
    }
<#

.SYNOPSIS A concatenation of Get-History/Invoke-History in one command. Display last cmdCnt number of commands. Allow user to select by number and run.

.PARAMETER cmdCnt (Optional) Number of commands to display. Default = 15.

#>
}
Set-Alias rch Run-CmdFromHistory -Scope “Global”

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? * Time limit is exhausted. Please reload CAPTCHA.

Protected with IP Blacklist CloudIP Blacklist Cloud