Skip to content

Commit 39d9e7a

Browse files
committed
Create Get-ClipboardHistory.ps1
1 parent 4b82a1f commit 39d9e7a

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Get-ClipboardHistory.ps1

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<#
2+
3+
from: https://gist.githubusercontent.com/mutaguchi/019ad33e156637585a22a656d8fd3f46/raw/38cba2de838e52004e75c400c254877a4f8e6ad3/ClipboardHistory.ps1
4+
Get-ClipboardHistory: Get the texts contained in the clipboard history.
5+
Clear-ClipboardHistory: Clearing the clipboard history
6+
7+
In PowerShell 7.1 or later, use the following command to install Microsoft.Windows.SDK.NET.Ref with administrative privileges.
8+
Find-Package -ProviderName NuGet -Source https://www.nuget.org/api/v2 -Name Microsoft.Windows.SDK.NET.Ref | Install-Package
9+
#>
10+
11+
$needsSDK = $PSVersionTable.PSVersion -ge "7.1.0"
12+
13+
if ($needsSDK) {
14+
$sdkLib = Split-Path -Path (Get-Package -ProviderName NuGet -Name Microsoft.Windows.SDK.NET.Ref | Select-Object -ExpandProperty Source) -Parent | Join-Path -ChildPath "\lib"
15+
Add-Type -Path "$sdkLib\Microsoft.Windows.SDK.NET.dll"
16+
Add-Type -Path "$sdkLib\WinRT.Runtime.dll"
17+
}
18+
else {
19+
Add-Type -AssemblyName System.Runtime.WindowsRuntime
20+
}
21+
22+
$clipboard = if ($needsSDK) {
23+
[Windows.ApplicationModel.DataTransfer.Clipboard]
24+
}
25+
else {
26+
[Windows.ApplicationModel.DataTransfer.Clipboard, Windows.ApplicationModel.DataTransfer, ContentType = WindowsRuntime]
27+
}
28+
get-
29+
function await {
30+
param($AsyncTask, [Type]$ResultType)
31+
32+
$method = [WindowsRuntimeSystemExtensions].GetMember("GetAwaiter") | Where-Object { $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' } | Select-Object -First 1 $method.MakeGenericMethod($ResultType).Invoke($null, @($AsyncTask)).GetResult()
33+
}
34+
35+
function Get-ClipboardHistory {
36+
$type = if ($script:needsSDK) {
37+
[Windows.ApplicationModel.DataTransfer.ClipboardHistoryItemsResult]
38+
}
39+
else {
40+
[Windows.ApplicationModel.DataTransfer.ClipboardHistoryItemsResult, Windows.ApplicationModel.DataTransfer, ContentType = WindowsRuntime]
41+
}
42+
43+
$result = await $script:clipboard::GetHistoryItemsAsync() $type
44+
45+
$outItems = if ($script:needsSDK) {
46+
@($result.Items.AdditionalTypeData.Values)
47+
}
48+
else {
49+
@($result.Items)
50+
}
51+
52+
$outItems | Where-Object { $_.Content.Contains("Text") } | ForEach-Object {
53+
await $_.Content.GetTextAsync() ([string])
54+
}
55+
}
56+
57+
function Clear-ClipboardHistory {
58+
$null = $script:clipboard::ClearHistory()
59+
}

0 commit comments

Comments
 (0)