Skip to content

Instantly share code, notes, and snippets.

@kiennq
Created November 22, 2023 06:50
Show Gist options
  • Save kiennq/aa12ddc05023e590e327c8e0de2df82e to your computer and use it in GitHub Desktop.
Save kiennq/aa12ddc05023e590e327c8e0de2df82e to your computer and use it in GitHub Desktop.
Get Clipboard content in via WinRT API as powershell script
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
$null = [Windows.ApplicationModel.DataTransfer.Clipboard, Windows.ApplicationModel.DataTransfer, ContentType=WindowsRuntime]
$content = [Windows.ApplicationModel.DataTransfer.Clipboard]::GetContent()
$formats = $content.AvailableFormats;
$formats | ForEach-Object -Process {
echo $_
Await ($content.GetTextAsync($_)) ([String])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment