{"id":11471,"date":"2012-01-15T00:01:00","date_gmt":"2012-01-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/01\/15\/use-powershell-to-choose-unique-objects-from-a-sorted-list\/"},"modified":"2012-01-15T00:01:00","modified_gmt":"2012-01-15T00:01:00","slug":"use-powershell-to-choose-unique-objects-from-a-sorted-list","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-choose-unique-objects-from-a-sorted-list\/","title":{"rendered":"Use PowerShell to Choose Unique Objects from a Sorted List"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to select unique objects and properties from a sorted list.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I have been busy working on the study guide for the 2012 Scripting Games. It will look similar to the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/02\/11\/2011-scripting-games-study-guide.aspx\" target=\"_blank\">2011 Scripting Games Guide<\/a>. If you are thinking about the 2012 Scripting Games, and you really should, you would do well to review the content I created for the 2011 Scripting Games Guide. Also think about the following help to ramp up for this year&rsquo;s games:<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/2011-scripting-games-all-links-on-one-page\/\" target=\"_blank\">Review the event questions and answers<\/a><\/li>\n<li><a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/dd742419.aspx\" target=\"_blank\">Watch the five training videos<\/a><\/li>\n<li><a href=\"http:\/\/quizapp.cloudapp.net\/powershell.aspx\" target=\"_blank\">Take the Windows PowerShell quizzes<\/a><\/li>\n<\/ul>\n<p>The Scripting Games are the best opportunity to learn Windows PowerShell.<\/p>\n<h2>Using the Get-Unique cmdlet<\/h2>\n<p>Anyway, as part of my work for the 2012 Scripting Games, I have been looking over cmdlets, examining the Help files, and in general reviewing the Windows PowerShell fundamentals. It dawned on me that I have not written very much about the <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=get-unique&amp;sections=7618\" target=\"_blank\">Get-Unique<\/a> cmdlet.<\/p>\n<p>It is important to keep in mind how the <b>Get-Unique <\/b>cmdlet works, or else it can return misleading results. For example, the following code that returns the number of unique processes running on a computer appears to work just fine.<\/p>\n<p style=\"padding-left: 30px\">Get-Process | Get-Unique | Measure-Object<\/p>\n<h2>Sort the list of objects<\/h2>\n<p>Unfortunately, there is no guarantee that the results will be accurate. This is because the Get-Unique cmdlet compares each item in a sorted list to the next item to eliminate duplicates. Without sorting the list, the results are not necessarily going to be accurate.<\/p>\n<p>There is another issue to consider: How do I determine what is unique in dealing with processes? Do I want unique process names, unique process IDs, unique memory consumption, unique executable paths, or what? The <i>System.Diagnostics.Process <\/i>.NET Framework object contains 51 properties&mdash;any one of which one could use to define &ldquo;unique.&rdquo; Without specifics, one cannot be certain what unique characters will return for the object.<\/p>\n<p>An additional aspect of uniqueness to consider arises because of the object-oriented nature of Windows PowerShell. The <b>Get-Unique <\/b>cmdlet returns unique objects, or unique strings. Because everything in Windows PowerShell is an object, this can become an extremely short list. The following code returns only one unique object from a listing of processes on the local computer.<\/p>\n<p style=\"padding-left: 30px\">Get-Process | Get-Unique -OnType | Measure-Object<\/p>\n<h2>Specify the property upon which to sort<\/h2>\n<p>When examining a specific property of an object and treating the property as a string, the <b>Get-Unique <\/b>cmdlet determines the property upon which to operate based on the property that is specified to the <b>Sort-Object <\/b>cmdlet. The following code counts the number of unique processes, based on process name.<\/p>\n<p style=\"padding-left: 30px\">Get-Process | sort-object name | Get-Unique -asstring | Measure-Object<\/p>\n<p>To identify unique processes based on the process ID, use the code that follows.<\/p>\n<p style=\"padding-left: 30px\">Get-Process | sort-object id | Get-Unique -asstring | Measure-Object<\/p>\n<p>The commands to derive unique processes based on object type, process name, and process ID, along with the output associated with each command, appear in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5857.HSG-01-15-12-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5857.HSG-01-15-12-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>A shortcut to uniqueness<\/h2>\n<p>In most cases, it is a unique property and not a unique object that is the object of a query. Because the <b>Get-Unique <\/b>cmdlet requires sorting objects prior to piping, it is possible to bypass use of the <b>Get-Unique <\/b>cmdlet by using the <i>Unique <\/i>switched parameter from the <b>Sort-Object <\/b>cmdlet. Here is an example of using the <i>Unique <\/i>parameter from the <b>Sort-Object <\/b>cmdlet.<\/p>\n<p style=\"padding-left: 30px\">get-service | Sort-Object status -Unique | measure<\/p>\n<p>If you use the <b>Select-Object <\/b>cmdlet, it might be easier to use the <i>Unique <\/i>parameter from that cmdlet. The code to do this is shown here.<\/p>\n<p style=\"padding-left: 30px\">get-service | Select-object status -unique | measure<\/p>\n<h2>What about case sensitivity?<\/h2>\n<p>Nearly everything in Windows PowerShell defaults to case insensitive, therefore it might come as a surprise that the <b>Get-Unique <\/b>cmdlet and the <b>Select-Object <\/b>cmdlet are case sensitive in determining uniqueness. The <b>Sort-Object <\/b>cmdlet is not case sensitive when choosing unique objects from the list.<\/p>\n<p>In the code that follows, I create an array of strings with a mixture of upper case and lowercase items in the array. I then pipe the strings to the <b>Sort-Object<\/b> cmdlet prior to piping the results to the <b>Get-Unique<\/b> cmdlet to sort the strings. Next, I pipe the strings to the <b>Sort-Object<\/b> and use the <i>Unique <\/i>parameter to obtain uniqueness. Finally, I pipe the strings to the <b>Select-Object<\/b> cmdlet, and I use the <i>Unique <\/i>parameter there also.<\/p>\n<p style=\"padding-left: 30px\">$a = &#8220;one&#8221;,&#8221;two&#8221;, &#8220;Two&#8221;, &#8220;three&#8221;, &#8220;Three&#8221;<\/p>\n<p style=\"padding-left: 30px\">$a | sort-object | Get-Unique &ndash;AsString<\/p>\n<p style=\"padding-left: 30px\">$a | sort-object &ndash;Unique<\/p>\n<p style=\"padding-left: 30px\">$a | Select-Object -Unique<\/p>\n<p>The code to create the array of strings and select unique strings from the array, and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3644.HSG-01-15-12-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3644.HSG-01-15-12-02.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Well, that is about all there is to selecting unique objects from a list. There are three ways to do this: use the <b>Get-Unique <\/b>cmdlet, use the <i>Unique <\/i>parameter from the <b>Sort-Object <\/b>cmdlet, or use the <i>Unique <\/i>parameter from the <b>Select-Object <\/b>cmdlet.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to select unique objects and properties from a sorted list. Microsoft Scripting Guy, Ed Wilson, is here. I have been busy working on the study guide for the 2012 Scripting Games. It will look similar to the 2011 Scripting Games Guide. If you [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,61,45],"class_list":["post-11471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to select unique objects and properties from a sorted list. Microsoft Scripting Guy, Ed Wilson, is here. I have been busy working on the study guide for the 2012 Scripting Games. It will look similar to the 2011 Scripting Games Guide. If you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11471"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11471\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}