{"id":107894,"date":"2023-03-03T07:00:00","date_gmt":"2023-03-03T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107894"},"modified":"2023-03-03T07:21:16","modified_gmt":"2023-03-03T15:21:16","slug":"20230303-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230303-00\/?p=107894","title":{"rendered":"Enumerating Windows clipboard history in PowerShell"},"content":{"rendered":"<p>Last time, <a title=\"Enumerating Windows clipboard history in C++\/WinRT and C#\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230302-00\/?p=107889\"> we enumerated the contents of the Windows clipboard history from C++\/WinRT and C#<\/a>. Today we&#8217;ll do it from PowerShell.<\/p>\n<p>Because, hey, why not.<\/p>\n<pre>$null = [Windows.ApplicationModel.DataTransfer.Clipboard, Windows.ApplicationModel.DataTransfer, ContentType=WindowsRuntime]\r\n$op = [Windows.ApplicationModel.DataTransfer.Clipboard]::GetHistoryItemsAsync()\r\n\r\n$result = <a href=\"https:\/\/stackoverflow.com\/a\/57896590\/902497\">Await<\/a> ($op) `\r\n    ([Windows.ApplicationModel.DataTransfer.ClipboardHistoryItemsResult])\r\n\r\n$textops = $result.Items.Content.GetTextAsync()\r\nfor ($i = 0; $i -lt $textops.Count; $i++){ Await($textops[$i]) ([String]) }\r\n<\/pre>\n<p>It&#8217;s basically the same thing we&#8217;ve been doing, just written in PowerShell. Note that I&#8217;m not a PowerShell expert, so some of the things I&#8217;m doing may be suboptimal.<\/p>\n<p>First, we load the Clipboard class into memory, specifying that it is a Windows Runtime class.<\/p>\n<p>Next, we call <code>Get\u00adHistory\u00adItems\u00adAsync<\/code> and <code>Await<\/code> it to get the history items result.<\/p>\n<p>We take the <code>Items<\/code> from the result, get their <code>Content<\/code>, and ask each <code>Content<\/code> for its text.<\/p>\n<p>The text query is another asynchronous operation, so we iterate through the operations and <code>Await<\/code> each one, sending the results back into the pipeline.<\/p>\n<p>I didn&#8217;t add the code to check ahead of time whether the content contained text. I just let the exception flow out of the <code>Get\u00adText\u00adAsync<\/code> call. Fixing this is left as an exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Doing Windows Runtime things from PowerShell.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-107894","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Doing Windows Runtime things from PowerShell.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=107894"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107894\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=107894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}