jdhitsolutions / PSScriptTools

:wrench: :hammer: A set of PowerShell functions you might use to enhance your own functions and scripts or to facilitate working in the console. Most should work in both Windows PowerShell and PowerShell 7, even cross-platform. Any operating system limitations should be handled on a per command basis. The Samples folder contains demonstration script files
MIT License
901 stars 112 forks source link

Copy-HistoryCommand could benefit from accepting value from pipeline #119

Closed corbob closed 2 years ago

corbob commented 2 years ago

Is your feature request related to a problem? Please describe. I love the idea of this function, but I find myself often wanting to copy a series of commands.

Describe the solution you'd like I would like to be able to issue a command like 10..15 | ch and have the clipboard contain the contents of all of these commands.

Describe alternatives you've considered

I have a snippet like this that I use today: 10..15 | h | % commandline | scb

Additional context I did some playing around to prove out the concept, and I think it can be done with minor(ish) changes. The code that I have working is:

Function Copy-HistoryCommand {

    [CmdletBinding(SupportsShouldProcess)]
    [alias("ch")]
    [outputtype("None", "System.String")]
    Param(
        [Parameter(Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [int]$ID = $(Get-History).Count,
        [switch]$Passthru)

    Begin {
        Write-Verbose "[BEGIN  ] Starting: $($MyInvocation.Mycommand)"
        $commands = @()
    } #begin

    Process {
        Write-Verbose "[PROCESS] Getting commandline from history item: $id"
        $commands += (Get-History -Id $id).CommandLine

    } #process

    End {
        If ($PSCmdlet.ShouldProcess("ID #$id [$commands]")) {
            $commands | Microsoft.PowerShell.Management\Set-Clipboard

            If ($Passthru) {
                #write the command to the pipeline
                $commands
            } #If passthru
        }
        Write-Verbose "[END    ] Ending: $($MyInvocation.Mycommand)"
    } #end

} #close function

Obviously the use of an array is problematic from a performance perspective, but for a quick and dirty proof of concept it works fairly well.

jdhitsolutions commented 2 years ago

I was having the same thought about copying multiple commands. I'm still working on the next module release and plan on adding this functionality to the command.

jdhitsolutions commented 2 years ago

This is fixed in v2.41.0

jdhitsolutions commented 2 years ago

The latest version doesn't accept pipeline input, but you could easily run a command like copy-historycommand (10..15)

corbob commented 2 years ago

The latest version doesn't accept pipeline input, but you could easily run a command like copy-historycommand (10..15)

This is true. It actually makes it easier for my use because my keyboard makes it super easy to insert () compared to | 👍