{"id":946,"date":"2017-03-21T21:45:52","date_gmt":"2017-03-21T19:45:52","guid":{"rendered":"http:\/\/www.netexpertise.eu\/en\/?p=946"},"modified":"2021-06-02T08:40:48","modified_gmt":"2021-06-02T06:40:48","slug":"count-results-in-powershell","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html","title":{"rendered":"Powershell Count Returns Incorrect Values"},"content":{"rendered":"\n<p>Powershell provides a &#8220;count&#8221; method that comes first to mind when you have to count lines, files or objects. I was astonished to observe <a href=\"\/en\/tag\/powershell\">PowerShell<\/a> would display nothing when it should count 0 or 1.<br>\u00a0<br>Here&#8217;s a screenshot where I retrieve the number of Active Directory accounts and showing count is unreliable.<br><br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"http:\/\/www.netexpertise.eu\/images\/PowerShell_count_01.jpg\" alt=\"Powershell incorrect count\"\/><\/figure><\/div>\n\n\n\n<p><br>Instead use the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/measure-object\" target=\"_blank\" rel=\"noreferrer noopener\">Measure-Object<\/a> cmdLet and select the count value that is returned:<br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">C:\\>(Get-ADUser -filter {SamAccountName -like \"dron*\"} |\n     select SamAccountName | measure).count\n1<\/code><\/pre>\n\n\n\n<p>\u00a0<br>In addition to the Measure-Object command-let that counts the number of objects, you can use the &#8220;<a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/tee-object\" target=\"_blank\" rel=\"noreferrer noopener\">tee<\/a>&#8221; command that lets you store the result set into a variable and its number of elements into another. All of this in one single line:<br>\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">C:\\>$accountnb = (Get-ADUser -filter {SamAccountName -like \"dr*\"} |\n                 select SamAccountName |\n                 tee -Variable accounts | measure).count\n\nC:\\>$accountnb\n3\n\nC:\\>$accounts\n\nSamAccountName\n--------------\ndraguene\ndrondy\ndroze<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Powershell provides a &#8220;count&#8221; method that comes first to mind when you have to count lines, files or objects. I was astonished to observe PowerShell would display nothing when it should count 0 or 1.\u00a0Here&#8217;s a screenshot where I retrieve the number of Active Directory accounts and showing count is unreliable. Instead use the Measure-Object [&hellip;]<\/p>\n","protected":false},"author":2,"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},"categories":[70],"tags":[383,391],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Powershell Count Returns Incorrect Values<\/title>\n<meta name=\"description\" content=\".count in Powershell seems to be giving unreliable results. Here is how to get proper count results and full result set in one single line\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Powershell Count Returns Incorrect Values\" \/>\n<meta property=\"og:description\" content=\".count in Powershell seems to be giving unreliable results. Here is how to get proper count results and full result set in one single line\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-21T19:45:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-02T06:40:48+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.netexpertise.eu\/images\/PowerShell_count_01.jpg\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html\",\"name\":\"Netexpertise - Powershell Count Returns Incorrect Values\",\"isPartOf\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2017-03-21T19:45:52+00:00\",\"dateModified\":\"2021-06-02T06:40:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\".count in Powershell seems to be giving unreliable results. Here is how to get proper count results and full result set in one single line\",\"breadcrumb\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Powershell Count Returns Incorrect Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Powershell Count Returns Incorrect Values","description":".count in Powershell seems to be giving unreliable results. Here is how to get proper count results and full result set in one single line","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:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Powershell Count Returns Incorrect Values","og_description":".count in Powershell seems to be giving unreliable results. Here is how to get proper count results and full result set in one single line","og_url":"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html","og_site_name":"Netexpertise","article_published_time":"2017-03-21T19:45:52+00:00","article_modified_time":"2021-06-02T06:40:48+00:00","og_image":[{"url":"http:\/\/www.netexpertise.eu\/images\/PowerShell_count_01.jpg"}],"author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html","url":"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html","name":"Netexpertise - Powershell Count Returns Incorrect Values","isPartOf":{"@id":"https:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2017-03-21T19:45:52+00:00","dateModified":"2021-06-02T06:40:48+00:00","author":{"@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":".count in Powershell seems to be giving unreliable results. Here is how to get proper count results and full result set in one single line","breadcrumb":{"@id":"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.netexpertise.eu\/en\/systems\/windows\/count-results-in-powershell.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Powershell Count Returns Incorrect Values"}]},{"@type":"WebSite","@id":"https:\/\/www.netexpertise.eu\/en\/#website","url":"https:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/946"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=946"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/946\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}