{"id":8465,"date":"2021-07-01T13:47:00","date_gmt":"2021-07-01T17:47:00","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8465"},"modified":"2021-07-01T13:47:04","modified_gmt":"2021-07-01T17:47:04","slug":"filtering-powershell-unique-objects","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/","title":{"rendered":"Filtering for Unique Objects in PowerShell"},"content":{"rendered":"\n<p>A few weeks ago my friend, Gladys Kravitz, was lamenting about a challenge related to filtering for unique objects. PowerShell has a Get-Unique cmdlet, and Select-Object has a -Unique parameter, but these options are limited. On one hand, I'd say most things we manage with PowerShell are guaranteed to be unique. Objects might have a GUID , ID, or SID, to guarantee uniqueness. But, and this is Gladys' situation, sometimes the things we are managing come from an external source. Such as importing data from a CSV file. In this situation, it is definitely possible to have duplicate objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Test Data<\/h2>\n\n\n\n<p>Here's a sample CSV source that I'll import into PowerShell.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$Obj\u00a0=\u00a0\"Animal,Snack,Color\nHorse,Quiche,Chartreuse\nCat,Doritos,Red\nCat,Pringles,Yellow\nDog,Doritos,Yellow\nDog,Doritos,Yellow\nRabbit,Pretzels,Green\nRabbit,Popcorn,Green\nMarmoset,Cheeseburgers,Black\nDog,Doritos,White\nDog,Doritos,White\nDog,Doritos,White\n\"\u00a0|\u00a0ConvertFrom-Csv<\/code><\/pre>\n\n\n\n<p>You can plainly see the duplicate objects.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png\"><img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"376\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png\" alt=\"\" class=\"wp-image-8466\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png 408w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30-300x276.png 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Existing Commands<\/h2>\n\n\n\n<p>Using Select-Object doesn't offer any help for this situation, even though you feel like your telling PowerShell, \"Select unique objects\".<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-11-12.png\"><img loading=\"lazy\" decoding=\"async\" width=\"413\" height=\"141\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-11-12.png\" alt=\"\" class=\"wp-image-8467\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-11-12.png 413w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-11-12-300x102.png 300w\" sizes=\"auto, (max-width: 413px) 100vw, 413px\" \/><\/a><\/figure>\n\n\n\n<p>This works fine on a single property.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-13-20.png\"><img loading=\"lazy\" decoding=\"async\" width=\"588\" height=\"261\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-13-20.png\" alt=\"\" class=\"wp-image-8468\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-13-20.png 588w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-13-20-300x133.png 300w\" sizes=\"auto, (max-width: 588px) 100vw, 588px\" \/><\/a><\/figure>\n\n\n\n<p>But that isn't the goal. Then I turned my attention to Get-Unique. But this isn't any better.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-15-02.png\"><img loading=\"lazy\" decoding=\"async\" width=\"286\" height=\"159\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-15-02.png\" alt=\"\" class=\"wp-image-8469\"\/><\/a><\/figure>\n\n\n\n<p>So I did what I'm always telling you, I re-read the help. You can get this to work if you tell the command to treat everything as a string.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-25-22.png\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"295\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-25-22.png\" alt=\"\" class=\"wp-image-8470\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-25-22.png 533w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-25-22-300x166.png 300w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/a><\/figure>\n\n\n\n<p>If you are dealing with flat and simple objects like my demo objects, this should get the job done. And even though the comparison is via a string, the output is the original object. But there is one potential gotcha: the comparison is case-sensitive. I also found that if the object had rich properties, like an array or hashtable, Get-Unique didn't always provide the expected results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Compare-Object<\/h2>\n\n\n\n<p>I decided to go down a different rabbit hole and use Compare-Object. This cmdlet lets you specify multiple properties to compare.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-39-31.png\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"270\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-39-31.png\" alt=\"\" class=\"wp-image-8471\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-39-31.png 705w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-39-31-300x115.png 300w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/a><\/figure>\n\n\n\n<p>Objects 3 and 4 are identical. My plan was to build a list of unique objects and then compare each input object. If the object wasn't already in the list, then add it. I first create a list object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">\u00a0$UniqueList\u00a0=\u00a0[System.Collections.Generic.list[object]]::new()<\/code><\/pre>\n\n\n\n<p>Then for each object, I need to test if it exists. The List object has an Exists() method which requires a predicate, which is essentially a scriptblock.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">if\u00a0($UniqueList.Exists(\u00a0{\u00a0-not\u00a0(Compare-Object\u00a0$args[0].psobject.properties.value\u00a0$item.psobject.Properties.value)\u00a0}))\u00a0{\n\u00a0\u00a0\u00a0\u00a0Write-Debug\u00a0\"[$((Get-Date).TimeofDay)\u00a0PROCESS]\u00a0Skipping:\u00a0$($item\u00a0|Out-String)\"\n}\nelse\u00a0{\n\u00a0\u00a0\u00a0\u00a0Write-Debug\u00a0\"[$((Get-Date).TimeofDay)\u00a0PROCESS]\u00a0Adding\u00a0as\u00a0unique:\u00a0$($item\u00a0|\u00a0Out-String)\"\n\u00a0\u00a0\u00a0\u00a0$UniqueList.add($item)\n}<\/code><\/pre>\n\n\n\n<p>The code is saying, \"Compare every object in the list, $args[0] with the input object, $item. If there is a match, then the object is already in the list so don't do anything. Otherwise, add it to the list.\" After processing all objects, $UniqueList should be the unique objects. And by the way, Compare-Object is not case-sensitive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get-PSUnique<\/h2>\n\n\n\n<p>I put all of this together into a function called Get-PSUnique.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-55-46.png\"><img loading=\"lazy\" decoding=\"async\" width=\"486\" height=\"305\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-55-46.png\" alt=\"\" class=\"wp-image-8472\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-55-46.png 486w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-55-46-300x188.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><\/a><\/figure>\n\n\n\n<p>In my testing, this command will handle simple arrays and hashtables for comparisons. I guess this limit is whatever Compare-Object has when it comes to comparing properties. Still, I'd recommend this function for dealing with simple objects.<\/p>\n\n\n\n<p>Want to try yourself? I've added this command to the latest version (2.28.0) of the <a href=\"https:\/\/github.com\/jdhitsolutions\/PSScriptTools\" target=\"_blank\" rel=\"noreferrer noopener\">PSScriptTools <\/a>module which you can install from the PowerShell Gallery. Or you are welcome to check out the code for <a href=\"https:\/\/github.com\/jdhitsolutions\/PSScriptTools\/blob\/master\/functions\/Get-PSUnique.ps1\">Get-PSUnique<\/a>. I hope you find this as useful as I think Gladys did.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago my friend, Gladys Kravitz, was lamenting about a challenge related to filtering for unique objects. PowerShell has a Get-Unique cmdlet, and Select-Object has a -Unique parameter, but these options are limited. On one hand, I&#8217;d say most things we manage with PowerShell are guaranteed to be unique. Objects might have a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Filtering for Unique Objects in #PowerShell","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[],"class_list":["post-8465","post","type-post","status-publish","format-standard","hentry","category-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Filtering for Unique Objects in PowerShell &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Filtering for Unique Objects in PowerShell &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-01T17:47:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-01T17:47:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Filtering for Unique Objects in PowerShell\",\"datePublished\":\"2021-07-01T17:47:00+00:00\",\"dateModified\":\"2021-07-01T17:47:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/\"},\"wordCount\":516,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/2021-07-01_12-09-30.png\",\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/\",\"name\":\"Filtering for Unique Objects in PowerShell &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/2021-07-01_12-09-30.png\",\"datePublished\":\"2021-07-01T17:47:00+00:00\",\"dateModified\":\"2021-07-01T17:47:04+00:00\",\"description\":\"An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/2021-07-01_12-09-30.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/2021-07-01_12-09-30.png\",\"width\":408,\"height\":376},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8465\\\/filtering-powershell-unique-objects\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Filtering for Unique Objects in PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Filtering for Unique Objects in PowerShell &#8226; The Lonely Administrator","description":"An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/","og_locale":"en_US","og_type":"article","og_title":"Filtering for Unique Objects in PowerShell &#8226; The Lonely Administrator","og_description":"An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-07-01T17:47:00+00:00","article_modified_time":"2021-07-01T17:47:04+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Filtering for Unique Objects in PowerShell","datePublished":"2021-07-01T17:47:00+00:00","dateModified":"2021-07-01T17:47:04+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/"},"wordCount":516,"commentCount":4,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png","articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/","name":"Filtering for Unique Objects in PowerShell &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png","datePublished":"2021-07-01T17:47:00+00:00","dateModified":"2021-07-01T17:47:04+00:00","description":"An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/07\/2021-07-01_12-09-30.png","width":408,"height":376},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8465\/filtering-powershell-unique-objects\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Filtering for Unique Objects in PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":8196,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8196\/comparing-powershell-property-names\/","url_meta":{"origin":8465,"position":0},"title":"Comparing PowerShell Property Names","author":"Jeffery Hicks","date":"February 24, 2021","format":false,"excerpt":"Recently, I was chatting with my friend Gladys Kravitz about the hassle of comparing objects in PowerShell. Even after all these years. She has a specific use case, but you might also feel the need for a better comparison option. And to be clear, the comparison we're talking about is\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/psobject-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/psobject-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/psobject-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/psobject-1.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/psobject-1.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/psobject-1.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":2009,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2009\/using-types-with-imported-csv-data-in-powershell\/","url_meta":{"origin":8465,"position":1},"title":"Using Types with Imported CSV Data in PowerShell","author":"Jeffery Hicks","date":"January 19, 2012","format":false,"excerpt":"The Import-CSV cmdlet in PowerShell is incredibly useful. You can take any CSV file and pump objects to the pipeline. The cmdlet uses the CSV header as properties for the custom object. PS S:\\> import-csv .\\testdata.csv Date : 1\/18\/2012 6:45:30 AM Name : Data_1 Service : ALG Key : 1\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4112,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4112\/scary-powershell\/","url_meta":{"origin":8465,"position":2},"title":"Scary PowerShell","author":"Jeffery Hicks","date":"October 31, 2014","format":false,"excerpt":"In honor of today's festivities, at least in the United States, I thought we'd look at some scary PowerShell. I have seen a lot of scary things in blog posts, tweets and forum discussions. Often these scary things are from people just getting started with PowerShell who simply haven't learned\u2026","rel":"","context":"In &quot;Best Practices&quot;","block_context":{"text":"Best Practices","link":"https:\/\/jdhitsolutions.com\/blog\/category\/best-practices\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/10\/103114_1654_ScaryPowerS1.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":8835,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8835\/powershell-7-consolegridview\/","url_meta":{"origin":8465,"position":3},"title":"PowerShell 7 ConsoleGridView","author":"Jeffery Hicks","date":"February 3, 2022","format":false,"excerpt":"Last night during my presentation for the Research Triangle PowerShell User Group, I briefly demonstrated a PowerShell 7 command called Out-ConsoleGridView. This command will not run in Windows PowerShell. If you haven't thought about running PowerShell 7, maybe you'll reconsider after learning more about this tool. You all know about\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/02\/eventlog-errors.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/02\/eventlog-errors.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/02\/eventlog-errors.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/02\/eventlog-errors.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":8409,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8409\/custom-csv-import-with-powershell\/","url_meta":{"origin":8465,"position":4},"title":"Custom CSV Import with PowerShell","author":"Jeffery Hicks","date":"May 18, 2021","format":false,"excerpt":"I am always looking for opportunities to use PowerShell in a way that adds value to my work. And hopefully yours. This is one of the reasons it is worth the time and effort to learn PowerShell. It can be used in so many ways beyond the out-of-the-box commands. Once\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/importcsv-customtype.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/importcsv-customtype.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/importcsv-customtype.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/importcsv-customtype.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1687,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1687\/filter-left\/","url_meta":{"origin":8465,"position":5},"title":"Filter Left","author":"Jeffery Hicks","date":"October 14, 2011","format":false,"excerpt":"When writing WMI queries expressions in Windows PowerShell, it is recommended to use WMI filtering, as opposed to getting objects and then filtering with Where-Object. I see expressions like this quite often: [cc lang=\"PowerShell\"] get-wmiobject win32_process -computer $c | where {$_.name -eq \"notepad.exe\"} [\/cc] In this situation, ALL process objects\u2026","rel":"","context":"In &quot;Best Practices&quot;","block_context":{"text":"Best Practices","link":"https:\/\/jdhitsolutions.com\/blog\/category\/best-practices\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8465"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8465\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}