forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-HistoryCommand.ps1
More file actions
44 lines (36 loc) · 1.39 KB
/
Copy pathCopy-HistoryCommand.ps1
File metadata and controls
44 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Function Copy-HistoryCommand {
[CmdletBinding(SupportsShouldProcess)]
[alias("ch")]
[outputtype("None", "System.String")]
Param(
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty()]
[int[]]$ID = $(Get-History).Count,
[switch]$Passthru)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
} #begin
Process {
$idString = $id -join ','
Write-Verbose "[PROCESS] Getting commandline from history item: $idstring"
$cmdstring = (Get-History -Id $($id)).CommandLine
If ($PSCmdlet.ShouldProcess("ID #$idstring")) {
$cmdstring | Microsoft.PowerShell.Management\Set-Clipboard
If ($Passthru) {
#write the command to the pipeline
$cmdstring
} #If passthru
}
} #process
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
} #close function
Register-ArgumentCompleter -CommandName Copy-HistoryCommand -ParameterName Id -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
Get-History | Where-Object { $_.id -like "$wordtocomplete*" } |
ForEach-Object {
# completion text,listitem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_.id, $_.id, 'ParameterValue', $_.commandline)
}
}