1
+ # requires -version 5.1
2
+ # requires -module PSReadline
3
+
4
+ Function Copy-HistoryCommand {
5
+ <#
6
+ . SYNOPSIS
7
+ Copy a history command line to the clipboard.
8
+ . DESCRIPTION
9
+ You can use this command to copy the commandline from a given PowerShell
10
+ history item to the clipboard. The default item will the be last history
11
+ item. Once copied, you can paste into your following prompt to edit and/or
12
+ re-run.
13
+
14
+ Linux platforms require the xclip utility to be in the path.
15
+
16
+ Lee Holmes has a similar function called Copy-History in the PowerShell
17
+ Cookbook that lets you copy a range of history commands to the clipboard.
18
+ . EXAMPLE
19
+ PS C:\> Copy-HistoryCommand
20
+
21
+ Copy the last command to the clipboard.
22
+ . EXAMPLE
23
+ PS C:\> Copy-HistoryCommand 25 -passthru
24
+ get-process -computername $computer | sort ws -Descending | select -first 3
25
+
26
+ Copy the command from history item 25 to the clipboard and also pass it to the pipeline.
27
+ . EXAMPLE
28
+ PS C:\> $c = [scriptblock]::Create($(Copy-HistoryCommand 25 -passthru))
29
+
30
+ This copies the command from history item 25 and turns it into a scriptblock.
31
+
32
+ PS C:\> &$c
33
+
34
+ Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id SI ProcessName
35
+ ------- ------ ----- ----- ----- ------ -- -- -----------
36
+ 10414 12744 488164 461596 ...76 3128 0 dns
37
+ 581 67 171868 141620 ...82 3104 0 MsMpEng
38
+ 678 48 118132 89572 840 7180 0 ServerManager
39
+
40
+ Invoke the scriptblock.
41
+
42
+ . PARAMETER ID
43
+ The history ID number. The default is the last command.
44
+ . PARAMETER Passthru
45
+ Use this parameter if you also want to see the command as well as copy it to the clipboard.
46
+ . INPUTS
47
+ [int]
48
+ . OUTPUTS
49
+ [string]
50
+ . NOTES
51
+ Learn more about PowerShell:
52
+ http://jdhitsolutions.com/blog/essential-powershell-resources/
53
+
54
+ . LINK
55
+ Get-History
56
+ #>
57
+
58
+ [CmdletBinding (SupportsShouldProcess )]
59
+ [alias (" ch" )]
60
+ [outputtype (" None" , " string" )]
61
+ Param (
62
+ [Parameter (Position = 0 )]
63
+ [ValidateNotNullOrEmpty ()]
64
+ [int ]$ID = $ (Get-History ).Count,
65
+ [switch ]$Passthru )
66
+
67
+ Begin {
68
+ Write-Verbose " [BEGIN ] Starting: $ ( $MyInvocation.Mycommand ) "
69
+ } # begin
70
+
71
+ Process {
72
+ Write-Verbose " [PROCESS] Getting commandline from history item: $id "
73
+ $cmdstring = (Get-History - id $id ).CommandLine
74
+ If ($PSCmdlet.ShouldProcess (" ID #$id [$cmdstring ]" )) {
75
+ $cmdstring | Microsoft.PowerShell.Management\Set-Clipboard
76
+
77
+ If ($Passthru ) {
78
+ # write the command to the pipeline
79
+ $cmdstring
80
+ } # If passthru
81
+ }
82
+ } # process
83
+
84
+ End {
85
+ Write-Verbose " [END ] Ending: $ ( $MyInvocation.Mycommand ) "
86
+ } # end
87
+
88
+ } # close function
89
+
90
+
91
+ Register-ArgumentCompleter - CommandName Copy-HistoryCommand - ParameterName Id - ScriptBlock {
92
+ param ($commandName , $parameterName , $wordToComplete , $commandAst , $fakeBoundParameter )
93
+
94
+ # PowerShell code to populate $wordtoComplete
95
+ Get-History | Where-object {$_.id -like " $wordtocomplete *" } |
96
+ ForEach-Object {
97
+ # completion text,listitem text,result type,Tooltip
98
+ [System.Management.Automation.CompletionResult ]::new($_.id , $_.id , ' ParameterValue' , $_.commandline )
99
+ }
100
+ }
0 commit comments