{"id":7494,"date":"2020-05-27T14:57:53","date_gmt":"2020-05-27T18:57:53","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=7494"},"modified":"2020-05-27T17:13:44","modified_gmt":"2020-05-27T21:13:44","slug":"solving-the-powershell-counting-challenge","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/","title":{"rendered":"Solving the PowerShell Counting Challenge"},"content":{"rendered":"<p>A few weeks ago, an Iron Scripter PowerShell <a href=\"https:\/\/ironscripter.us\/a-powershell-counting-challenge\/\" target=\"_blank\" rel=\"noopener noreferrer\">scripting challenge<\/a> was posted. As with all of these challenges, the process is more important than the end result. How you figure out a solution is how you develop as a PowerShell professional. A few people have already <a href=\"https:\/\/powershell.anovelidea.org\/powershell\/powershell-counting-challenge\/\" target=\"_blank\" rel=\"noopener noreferrer\">shared their work<\/a>. Today, I thought I'd share mine.<\/p>\n<h2>Beginner<\/h2>\n<p>The beginner challenge was to get the sum of even numbers between 1 and 100. You should be able to come up with at least 3 different techniques.<\/p>\n<h3>For Loop<\/h3>\n<p>My first solution is to use a For loop.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t = 0\nfor ($i = 2; $i -le 100; $i += 2) {\n    $t += $i\n}\n$t\n<\/pre>\n<p>The variable $t is set to 0. This will be the total value. The syntax of the For loop says, \"Start with $i and a value of 2 and keep looping while $i is less than or equal to 100. Every time you loop, increase the value of $i by 2.\" The += operator is a shortcut way of saying $i = $i+2. This should get all even numbers up to and including 100. I'm using the -le operator and not the -lt operator. Each time through the loop, the code inside the { }, I'm incrementing $t by the value of $i. In the end, I get a value of 2550 for $t.<\/p>\n<h3>Modulo Operator<\/h3>\n<p>The next technique is to use the Modulo operator - %.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">1..100 | Where-Object {-Not($_%2)} | Measure-Object -sum\n<\/pre>\n<p>The first part of the pipelined expression is using the Range operator to get all numbers between 1 and 100. Each number is piped to Where-Object which uses the Modulo operator. I'm dividing each number by 2. If there is a remainder, for example, 3\/2 is 1.5, the result is 1. Otherwise, the operator produces a result of 0. 1 and 0 can also be interpreted as boolean values. 1 is $True and 0 is $False. In this case, all the numbers divided by 2 with no remainder will produce a value of 0. But I need Where-Object to pass objects when the expression is True so I use the -Not operator to reverse the value. Thus False becomes True and the number is passed to Measure-Object where I can get the sum. And yes, there are other ways you could have written the Where-Object expression.<\/p>\n<h3>ForEach Loop<\/h3>\n<p>My third idea was to use a ForEach loop.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$t = 0\nforeach ($n in (1..100)) {\n    if ($n\/2 -is [int]) {\n        $t += $n\n    }\n}\n$t\n<\/pre>\n<p>The code says, \"Foreach thing\u00a0 in the collection, do something.\" The collection is the range 1..100. The \"thing\" can be called anything you want. In my code, this is $n.\u00a0 For each value, I'm using an IF statement to test if $n\/2 is an object of type [int]. A value like 1.5 is technically a [double]. Assuming the value passes the test, I increment $t by that value.<\/p>\n<h3>Bonus<\/h3>\n<p>The \"right\" solution depends on the rest of your code and what makes the most sense. That's why there are different techniques for you to learn.\u00a0 If I just wanted a simple one-line solution, I could do something like this:<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">(1..100 | where-object {$_\/2 -is [int]} | measure-object -sum).sum\n<\/pre>\n<p>Instead of getting the measurement object, I'm getting just the sum property. The ( ) tell PowerShell, \"run this code and hold on to the object(s)\".\u00a0 This is a one-line version of this:<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$m = 1..100 | where-object {$_\/2 -is [int]} | measure-object -sum\n$m.sum\n<\/pre>\n<p>With the (), $m is implicitly being defined.<\/p>\n<h2>Intermediate<\/h2>\n<p>The next level challenge was to write a function that would get the sum of every X value between 1 and a user-specified maximum. We were also asked to get the average and ideally include all the numbers that were used in the calculation.<\/p>\n<p>For development purposes, I always start with simple code. I tested getting the sum of every 6th number between 1 and 100.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">$total = 0\n$max = 100\n$int = 6\n$count = 0\n$Capture = @()\n\nfor ($i = $int; $i -le $max; $i += $int) {\n    $count++\n    $Capture += $i\n    $total += $i\n}\n[pscustomobject]@{\n    Start    = 1\n    End      = $Max\n    Interval = $int\n    Sum      = $total\n    Average  = $total\/$count\n    Values   = $Capture\n}\n<\/pre>\n<p>You'll see that I'm using similar operators and techniques. The new step is that I am creating a custom object on the fly. The hashtable keys become the property names.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">Start    : 1\nEnd      : 100\nInterval : 6\nSum      : 816\nAverage  : 51\nValues   : {6, 12, 18, 24...}\n<\/pre>\n<p>With this, I was able to create this function.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">Function Get-NFactorial {\n\n    [CmdletBinding()]\n\n    Param  (\n        [Parameter(Position = 0)]\n        [int32]$Start = 1,\n        [Parameter(Mandatory, Position = 1)]\n        [int32]$Maximum,\n        [Parameter(Mandatory)]\n        [ValidateRange(1, 10)]\n        [ArgumentCompleter({1..10})]\n        [int32]$Interval\n    )\n\n    Begin {\n        Write-Verbose \"Starting $($MyInvocation.Mycommand)\"\n        Write-Verbose \"Getting NFactorial values between $start and $Maximum\"\n        Write-Verbose \"Using an interval of $Interval\"\n\n        #initialize some variables\n        $count = 0\n        $Capture = @()\n        $Total = 0\n    } #begin\n\n    Process {\n        Write-Verbose \"Looping through the range of numbers\"\n        for ($i = $Interval; $i -le $Maximum; $i += $Interval) {\n            $count++\n            $Capture += $i\n            $Total += $i\n        }\n        Write-Verbose \"Writing result to the pipeline\"\n        [pscustomobject]@{\n            PSTypeName  = \"nFactorial\"\n            Start       = $Start\n            End         = $Maximum\n            Interval    = $Interval\n            Sum         = $Total\n            Average     = $total\/$count\n            Values      = $Capture\n            ValuesCount = $Capture.Count\n        }\n    } #process\n\n    End {\n        Write-Verbose \"Ending $($MyInvocation.Mycommand)\"\n    } #end\n\n} #close function\n<\/pre>\n<p>For the Interval parameter, I'm doing something you may not have seen before. I'm limiting the user to using an interval value between 1 and 10. That's the ValidateRange attribute you may have seen before. The other element is an ArgumentCompleter. When someone runs the function, I want them to be able to tab-complete the value for -Interval. I've noticed that when you use ValidateSet, tab completion works. But not with ValidateRange. (Yes, I could have used ValidateSet,\u00a0 \u00a0but then I wouldn't have anything to teach you!). The ArgumentCompleter uses the results of the code inside the {} as autocomplete values.<\/p>\n<p>Here's the function in action.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">PS C&gt;\\&gt; Get-Nfactorial -Start 1 -Maximum 100 -Interval 2\n\nStart       : 1\nEnd         : 100\nInterval    : 2\nSum         : 2550\nAverage     : 51\nValues      : {2, 4, 6, 8...}\nValuesCount : 50\n<\/pre>\n<p>Let's take this one more step.<\/p>\n<h3>Formatting Results<\/h3>\n<p>The default output is a list and maybe I don't really need to see all of these properties by default. If you noticed, in the custom object hashtable I defined a property called PSTypeName. In order to do custom formatting, your object needs a unique type name. Mine is called nFactorial.<\/p>\n<p>Formatting is going to require a ps1xml file. But don't freak out. Install the PSScriptTools module from the PowerShell Gallery and use New-PSFormatXML.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">Get-Nfactorial -Start 1 -Maximum 100 -Interval 2 | New-PSFormatXML -Path .\\nfactorial.format.ps1xml -Properties Start,End,Interval,Sum,Average -FormatType Table\n<\/pre>\n<p>You can edit the file and adjust it as needed. Here's my version.<\/p>\n<pre class=\"lang:xml mark:0 decode:true\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!--\nformat type data generated 05\/19\/2020 10:06:19 by BOVINE320\\Jeff\n--&gt;\n&lt;Configuration&gt;\n  &lt;ViewDefinitions&gt;\n    &lt;View&gt;\n      &lt;!--Created 05\/19\/2020 10:06:19 by BOVINE320\\Jeff--&gt;\n      &lt;Name&gt;default&lt;\/Name&gt;\n      &lt;ViewSelectedBy&gt;\n        &lt;TypeName&gt;nFactorial&lt;\/TypeName&gt;\n      &lt;\/ViewSelectedBy&gt;\n      &lt;TableControl&gt;\n        &lt;!--Delete the AutoSize node if you want to use the defined widths.\n        &lt;AutoSize \/&gt; \n        --&gt;\n        &lt;TableHeaders&gt;\n          &lt;TableColumnHeader&gt;\n            &lt;Label&gt;Start&lt;\/Label&gt;\n            &lt;Width&gt;5&lt;\/Width&gt;\n            &lt;Alignment&gt;left&lt;\/Alignment&gt;\n          &lt;\/TableColumnHeader&gt;\n          &lt;TableColumnHeader&gt;\n            &lt;Label&gt;End&lt;\/Label&gt;\n            &lt;Width&gt;6&lt;\/Width&gt;\n            &lt;Alignment&gt;right&lt;\/Alignment&gt;\n          &lt;\/TableColumnHeader&gt;\n          &lt;TableColumnHeader&gt;\n            &lt;Label&gt;Interval&lt;\/Label&gt;\n            &lt;Width&gt;8&lt;\/Width&gt;\n            &lt;Alignment&gt;center&lt;\/Alignment&gt;\n          &lt;\/TableColumnHeader&gt;\n          &lt;TableColumnHeader&gt;\n            &lt;Label&gt;Sum&lt;\/Label&gt;\n            &lt;Width&gt;9&lt;\/Width&gt;\n            &lt;Alignment&gt;right&lt;\/Alignment&gt;\n          &lt;\/TableColumnHeader&gt;\n          &lt;TableColumnHeader&gt;\n            &lt;Label&gt;Average&lt;\/Label&gt;\n            &lt;Width&gt;10&lt;\/Width&gt;\n            &lt;Alignment&gt;right&lt;\/Alignment&gt;\n          &lt;\/TableColumnHeader&gt;\n        &lt;\/TableHeaders&gt;\n        &lt;TableRowEntries&gt;\n          &lt;TableRowEntry&gt;\n            &lt;TableColumnItems&gt;\n              &lt;TableColumnItem&gt;\n                &lt;PropertyName&gt;Start&lt;\/PropertyName&gt;\n              &lt;\/TableColumnItem&gt;\n              &lt;TableColumnItem&gt;\n                &lt;PropertyName&gt;End&lt;\/PropertyName&gt;\n              &lt;\/TableColumnItem&gt;\n              &lt;TableColumnItem&gt;\n                &lt;PropertyName&gt;Interval&lt;\/PropertyName&gt;\n              &lt;\/TableColumnItem&gt;\n              &lt;TableColumnItem&gt;\n                &lt;ScriptBlock&gt;\"{0:n0}\" -f $_.Sum&lt;\/ScriptBlock&gt;\n              &lt;\/TableColumnItem&gt;\n              &lt;TableColumnItem&gt;\n                &lt;PropertyName&gt;Average&lt;\/PropertyName&gt;\n              &lt;\/TableColumnItem&gt;\n            &lt;\/TableColumnItems&gt;\n          &lt;\/TableRowEntry&gt;\n        &lt;\/TableRowEntries&gt;\n      &lt;\/TableControl&gt;\n    &lt;\/View&gt;\n  &lt;\/ViewDefinitions&gt;\n&lt;\/Configuration&gt;\n<\/pre>\n<p>To use, you need to update PowerShell.<\/p>\n<pre class=\"lang:ps mark:0 decode:true\">Update-FormatData .\\nfactorial.format.ps1xml\n<\/pre>\n<p>With this in place, I now get nicely formatted output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7496\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg\" alt=\"nfactorial\" width=\"1112\" height=\"183\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg 1112w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial-300x49.jpg 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial-1024x169.jpg 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial-768x126.jpg 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial-850x140.jpg 850w\" sizes=\"auto, (max-width: 1112px) 100vw, 1112px\" \/><\/p>\n<p>Remember, this is only my new default if I don't tell PowerShell to do anything else. I can still run the function and pipe to Select-Object or Format-List.<\/p>\n<p>This silly function is clearly far from practical, but I can use these techniques and patterns in future projects. I hope you got something out of this and I encourage you to tackle the Iron Scripter challenges as they come along.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago, an Iron Scripter PowerShell scripting challenge was posted. As with all of these challenges, the process is more important than the end result. How you figure out a solution is how you develop as a PowerShell professional. A few people have already shared their work. Today, I thought I&#8217;d share mine&#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":"","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,8],"tags":[224,618,534,140,540],"class_list":["post-7494","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-function","tag-iron-scripter","tag-powershell","tag-ps1xml","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Solving the PowerShell Counting Challenge &#8226; The Lonely Administrator<\/title>\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\/7494\/solving-the-powershell-counting-challenge\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Solving the PowerShell Counting Challenge &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"A few weeks ago, an Iron Scripter PowerShell scripting challenge was posted. As with all of these challenges, the process is more important than the end result. How you figure out a solution is how you develop as a PowerShell professional. A few people have already shared their work. Today, I thought I&#039;d share mine....\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-27T18:57:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-27T21:13:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg\" \/>\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=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Solving the PowerShell Counting Challenge\",\"datePublished\":\"2020-05-27T18:57:53+00:00\",\"dateModified\":\"2020-05-27T21:13:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/\"},\"wordCount\":911,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/nfactorial.jpg\",\"keywords\":[\"Function\",\"Iron Scripter\",\"PowerShell\",\"ps1xml\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/\",\"name\":\"Solving the PowerShell Counting Challenge &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/nfactorial.jpg\",\"datePublished\":\"2020-05-27T18:57:53+00:00\",\"dateModified\":\"2020-05-27T21:13:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/nfactorial.jpg\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/nfactorial.jpg\",\"width\":1112,\"height\":183},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/7494\\\/solving-the-powershell-counting-challenge\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Solving the PowerShell Counting Challenge\"}]},{\"@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":"Solving the PowerShell Counting Challenge &#8226; The Lonely Administrator","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\/7494\/solving-the-powershell-counting-challenge\/","og_locale":"en_US","og_type":"article","og_title":"Solving the PowerShell Counting Challenge &#8226; The Lonely Administrator","og_description":"A few weeks ago, an Iron Scripter PowerShell scripting challenge was posted. As with all of these challenges, the process is more important than the end result. How you figure out a solution is how you develop as a PowerShell professional. A few people have already shared their work. Today, I thought I'd share mine....","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/","og_site_name":"The Lonely Administrator","article_published_time":"2020-05-27T18:57:53+00:00","article_modified_time":"2020-05-27T21:13:44+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg","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":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Solving the PowerShell Counting Challenge","datePublished":"2020-05-27T18:57:53+00:00","dateModified":"2020-05-27T21:13:44+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/"},"wordCount":911,"commentCount":2,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg","keywords":["Function","Iron Scripter","PowerShell","ps1xml","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/","name":"Solving the PowerShell Counting Challenge &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg","datePublished":"2020-05-27T18:57:53+00:00","dateModified":"2020-05-27T21:13:44+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/nfactorial.jpg","width":1112,"height":183},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7494\/solving-the-powershell-counting-challenge\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Solving the PowerShell Counting Challenge"}]},{"@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":9018,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/9018\/an-iron-scripter-warm-up-solution\/","url_meta":{"origin":7494,"position":0},"title":"An Iron Scripter Warm-Up Solution","author":"Jeffery Hicks","date":"May 6, 2022","format":false,"excerpt":"We just wrapped up the 2022 edition of the PowerShell+DevOps Global Summit. It was terrific to be with passionate PowerShell professionals again. The culmination of the event is the Iron Scripter Challenge. You can learn more about this year's event and winner here. But there is more to the Iron\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8107,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8107\/scripting-challenge-meetup\/","url_meta":{"origin":7494,"position":1},"title":"Scripting Challenge Meetup","author":"Jeffery Hicks","date":"February 1, 2021","format":false,"excerpt":"As you probably know, I am the PowerShell problem master behind the challenges from the Iron Scripter site. Solving a PowerShell scripting challenge is a great way to test your skills and expand your knowledge. The final result is merely a means to an end. How you get there and\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\/rubik.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":7489,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7489\/powershell-word-play\/","url_meta":{"origin":7494,"position":2},"title":"PowerShell Word Play","author":"Jeffery Hicks","date":"May 19, 2020","format":false,"excerpt":"A few weeks ago an Iron Scripter PowerShell challenge was issued that involved playing with words and characters. Remember, the Iron Scripter challenges aren't intended to create meaningful, production worthy code. They are designed to help you learn PowerShell fundamentals and scripting techniques. This particular challenge was aimed at beginner\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\/2020\/05\/doublechar.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7559,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7559\/an-expanded-powershell-scripting-inventory-tool\/","url_meta":{"origin":7494,"position":3},"title":"An Expanded PowerShell Scripting Inventory Tool","author":"Jeffery Hicks","date":"June 19, 2020","format":false,"excerpt":"The other day I shared my code that I worked up to solve an Iron Scripter PowerShell challenge. One of the shortcomings was that I didn't address a challenge to include a property that would indicate what file was using a given command. I also felt I could do better\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\/2020\/06\/get-psscriptinventory.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7471,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7471\/a-powershell-network-monitor\/","url_meta":{"origin":7494,"position":4},"title":"A PowerShell Network Monitor","author":"Jeffery Hicks","date":"May 12, 2020","format":false,"excerpt":"I hope you've been trying your hand at the scripting challenges being posted on the Iron Scripter website. The challenges are designed for individuals to do on their own to build up their PowerShell scripting skills. A few weeks ago, a challenge was posted to create a network monitoring tool\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\/2020\/05\/netperf-2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/netperf-2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/netperf-2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/netperf-2.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/netperf-2.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":8236,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8236\/solving-another-powershell-math-challenge\/","url_meta":{"origin":7494,"position":5},"title":"Solving Another PowerShell Math Challenge","author":"Jeffery Hicks","date":"March 22, 2021","format":false,"excerpt":"Last month, the Iron Scripter Chairman posted a \"fun\" PowerShell scripting challenge. Actually, a few math-related challenges . As with all these challenges, the techniques and concepts you use to solve the challenge are more important than the result itself. Here's how I approached the problems. Problem #1 The first\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\/03\/get-possiblesum.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get-possiblesum.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get-possiblesum.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7494","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=7494"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7494\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}