{"id":12441,"date":"2011-10-10T00:01:00","date_gmt":"2011-10-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/10\/cool-powershell-game-teaches-cmdlet-names\/"},"modified":"2011-10-10T00:01:00","modified_gmt":"2011-10-10T00:01:00","slug":"cool-powershell-game-teaches-cmdlet-names","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/cool-powershell-game-teaches-cmdlet-names\/","title":{"rendered":"Cool PowerShell Game Teaches Cmdlet Names"},"content":{"rendered":"<p><strong>Summary:<\/strong> Learn Windows PowerShell cmdlet names by taking the Dr. Scripto Challenge.<br \/>&nbsp;<br \/>Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are on a road trip in Canada. We had a wonderful time in Toronto with Windows PowerShell MVP Sean Kearney. Now we are heading to Ottawa where we plan to meet with Windows PowerShell MVP Kirk Munro. In addition to getting to talk to customers about Windows PowerShell and meeting with way cool Windows PowerShell MVPs, it also means we have access to the television. At home in Charlotte, North Carolina, we do not watch television, so in some ways it is sort of a treat. For example, the Scripting Wife has a couple of shows she likes to watch. As she was watching one particular show in which contestants receive letters and blank spaces and try to match a clue, it dawned on me that I could write that in Windows PowerShell.<\/p>\n<p>So here it is; the Dr. Scripto Windows PowerShell Challenge. The script is interesting, but it is also illustrative of an interesting way to manipulate strings. The techniques could possibly be applicable in a wide variety of situations.<\/p>\n<p style=\"padding-left: 30px\"><strong>DrScriptoChallenge.ps1<\/strong><br \/>$array = @()<br \/>$array = Get-Command -CommandType cmdlet | <br \/>ForEach-Object { $_.name.tostring() }<br \/>Foreach($cmdlet in $array)<br \/>{<br \/>&nbsp;$rndChar = get-random -InputObject ($cmdlet.tochararray()) -count ($cmdlet.length\/4)<br \/>&nbsp;foreach($l in $rndchar)<br \/>&nbsp;{<br \/>&nbsp; $cmdlet = $cmdlet.Replace($l,&#8221;_&#8221;)<br \/>&nbsp;}# end foreach $l<br \/>&nbsp;$cmdlet<br \/>} #end foreach $cmdlet<\/p>\n<p>The first thing I do in the DrScriptoChallenge.ps1 script is create an empty array and store it in the <strong>$array<\/strong> variable. Next, I use the <strong>Get-Command<\/strong> cmdlet to retrieve all of the cmdlets, and I pipe the <strong>cmdletinfo<\/strong> objects to the <strong>Foreach-Object<\/strong> cmdlet where I obtain the name of each cmdlet as a string. I then store the resultant cmdlet names in the <strong>$array<\/strong> variable. All of this provides me with an array of Windows PowerShell cmdlet names. This portion of the code is shown here:<\/p>\n<p style=\"padding-left: 30px\">$array = @()<br \/>$array = Get-Command -CommandType cmdlet | <br \/>ForEach-Object { $_.name.tostring() }<\/p>\n<p>The next thing I want to do is to choose a random grouping of letters from each Windows PowerShell cmdlet. To do this, I turn the Windows PowerShell cmdlet names that are stored in the <strong>$array<\/strong> variable into an array of characters. This allows me to choose individual letters from the Windows PowerShell cmdlet names. I pass the cmdlet names to the <strong>Get-Random<\/strong> cmdlet to choose the random letters. One thing I do is look at the length of the Windows PowerShell cmdlet name, and if a cmdlet name is longer, I select additional random letters. To make the challenge harder, use a 2 or a 3 instead of a 4. To make it easier, use a 5 or a 6 instead of a 4. I store the resulting random letters in an array called <strong>$rndChar<\/strong>. This section of the Windows PowerShell script is shown here:<\/p>\n<p style=\"padding-left: 30px\">$rndChar = get-random -InputObject ($cmdlet.tochararray()) -count ($cmdlet.length\/4)<\/p>\n<p>Now I want to replace each of these random letters in the Windows PowerShell cmdlet names with an underscore character. I use a <strong>Foreach<\/strong> loop to walk through the array of letters, and the <strong>replace<\/strong> method to replace each letter with the underscore character. A key point here is to write the replacement string back to the original string, or else when the script completes, only the last letter will be replaced. This portion of the script is shown here:<\/p>\n<p style=\"padding-left: 30px\">foreach($l in $rndchar)<br \/>&nbsp;{<br \/>&nbsp; $cmdlet = $cmdlet.Replace($l,&#8221;_&#8221;)<br \/>&nbsp;}# end foreach $l<\/p>\n<p>The last thing to do is to display the modified string and close out the loop:<\/p>\n<p style=\"padding-left: 30px\">$cmdlet<br \/>} #end foreach $cmdlet<\/p>\n<p>Because there are several <strong>Foreach<\/strong> type of loops, I add a comment at the closing brace (curly bracket) to let me know the purpose of the brace. This makes troubleshooting easier. As the script currently stands, it simply displays the modified cmdlet names on the screen; to create a test simply redirect it to a text file as seen here:<\/p>\n<p style=\"padding-left: 30px\">$cmdlet &gt;&gt;c:\\fso\\DrScriptoChallenge.txt<\/p>\n<p>The script and associated output displayed on the screen are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3823.hsg-10-10-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of script and associated output\" alt=\"Image of script and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3823.hsg-10-10-11-1.png\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Well that is all there is to creating the DrScriptoChallenge. You might think the script is easier than the challenge&mdash;it is a great way to test your knowledge of the default Windows PowerShell cmdlets. Until tomorrow, keep on scripting.<\/p>\n<p>I invite you to follow me on <a title=\"Twitter\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a title=\"Facebook\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn Windows PowerShell cmdlet names by taking the Dr. Scripto Challenge.&nbsp;Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are on a road trip in Canada. We had a wonderful time in Toronto with Windows PowerShell MVP Sean Kearney. Now we are heading to Ottawa where we plan to meet with Windows [&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":[292,3,4,21,45],"class_list":["post-12441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hashtables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn Windows PowerShell cmdlet names by taking the Dr. Scripto Challenge.&nbsp;Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are on a road trip in Canada. We had a wonderful time in Toronto with Windows PowerShell MVP Sean Kearney. Now we are heading to Ottawa where we plan to meet with Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12441","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=12441"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12441\/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=12441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}