{"id":11551,"date":"2012-01-07T00:01:00","date_gmt":"2012-01-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/01\/07\/use-powershell-to-choose-a-specific-number-of-random-letters\/"},"modified":"2012-01-07T00:01:00","modified_gmt":"2012-01-07T00:01:00","slug":"use-powershell-to-choose-a-specific-number-of-random-letters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-choose-a-specific-number-of-random-letters\/","title":{"rendered":"Use PowerShell to Choose a Specific Number of Random Letters"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use the Windows PowerShell <b>Get-Random<\/b> cmdlet to choose a specific number of random letters.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. With the advent of the New Year, I am hard at work on the 2012 Scripting Games. This year there will be several improvements. I always learn from each year&rsquo;s games, and I am a firm believer in continuous improvement. Right now, I am busy working on selecting the ten domains that I will emphasize in this year&rsquo;s games. Two things will not change: the areas tested will relate to real world problems, and the beginner division will be for true beginners. Make no mistake about it&mdash;this is your chance to learn Windows PowerShell; to quote the title of my five-part webcast, &ldquo;Learn it now&mdash;before it is an emergency!&rdquo;<\/p>\n<h2>Creating random numbers<\/h2>\n<p>One of my favorite cmdlets is the Get-Random cmdlet; there are just so many times that I need to have a random number. Now I want to combine two things&mdash;letters and numbers. By using a trick from my recent blog, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/12\/25\/use-powershell-and-ascii-to-create-folders-with-letters.aspx\" target=\"_blank\">Use PowerShell and ASCII to Create Folders with Letters<\/a>,<i> <\/i>I can create random letters.<\/p>\n<p>There are two main ways that I use the <b>Get-Random<\/b> cmdlet. The first is to call the cmdlet and to let things fly. This is the default way of using the cmdlet, and it can be helpful in certain circumstances. For me, however, I generally need to provide some limits on the numbers that are returned by the <b>Get-Random<\/b> cmdlet. In these cases, I use the <i>Minimum <\/i>and the M<i>aximum <\/i>parameters to specify the range of numbers that are available to select. These two methods of utilizing the <b>Get-Random<\/b> cmdlet are shown here with the associated output from those two commands.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Random<\/p>\n<p style=\"padding-left: 30px\">465457929<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Random -Minimum 1 -Maximum 10<\/p>\n<p style=\"padding-left: 30px\">3<\/p>\n<p>When I was choosing the daily prize winners during the 2011 Scripting Games, I needed to have more than one random number selected, and I needed to specify the range of input. This is when I started experimenting with the <i>InputObject <\/i>parameter for the cmdlet. This second parameter set for the cmdlet combines a <i>Count <\/i>parameter with the <i>InputObject. <\/i>The <i>InputObject <\/i>parameter accepts an array of objects (and integers are objects) for input. Sweet! I now get to combine one of my favorite cmdlets with one of my favorite Windows PowerShell tricks (the range operator). This means that I can select three random numbers from an array of numbers that range from 1 to 100. The command to choose three random numbers from the numbers ranging from 1 to 100 is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Random -InputObject (1..100) -Count 3<\/p>\n<p>The big trick in the previous command is to use the parentheses to force the creation of the array prior to the selection of the three random numbers. The command that chooses three random numbers along with the associated output is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8132.hsg-1-7-12-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8132.hsg-1-7-12-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Combining random numbers and ASCII values<\/h2>\n<p>I want to be able to select random two letter combinations (it could be 1 or 100 random letters; it really does not matter). To do this, I want to use the way cool <b>Get-Random<\/b> cmdlet. The secret? Use the ASCII character values. In the previously mentioned blog, I needed to be able to increment letter values so I could add letters to automatically created folders, and I pointed out how to use the ASCII character values (numbers) and convert them to letters by using the <b>[char]<\/b> type accelerator.<\/p>\n<p>Instead of incrementing numbers in a certain range, I can just as easily randomly select the numbers. The ASCII values in the range of 65&ndash;90 are the capital letters A&ndash;Z. ASCII values 97&ndash;122 are the lower case letters a&ndash;z. To choose two random letters that will translate to ASCII capital letters in the range A&ndash;Z, I use the <b>Get-Random<\/b> cmdlet, specify a <i>Count <\/i>of 2, and provide a range operator to the <i>InputObject <\/i>parameter. The code to accomplish this is shown here, along with the associated output.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Random -Count 2 -InputObject (65..90)<\/p>\n<p style=\"padding-left: 30px\">80<\/p>\n<p style=\"padding-left: 30px\">89<\/p>\n<p>To convert the two randomly selected numbers to letters, I use the <b>Foreach-Object<\/b> cmdlet, and inside the script block, I use the <b>[char]<\/b> class to make the conversion. This command is shown here (I use the <b>% <\/b>alias for the <b>Foreach-Object<\/b> cmdlet).<\/p>\n<p style=\"padding-left: 30px\">Get-Random -Count 2 -InputObject (65..90) | % {[char]$_}<\/p>\n<p>The preceding commands, along the output associated with those commands are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4431.hsg-1-7-12-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4431.hsg-1-7-12-2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Use <i>Begin, Process<\/i>,<i> <\/i>and <i>End <\/i>to collect and display<\/h2>\n<p>So I am half-way there. I can get random letters, but the letters are coming one at a time&hellip;and I need to create random letter combinations of varying lengths. To do this, I am going to use the B<i>egin, Process, <\/i>and<i> End <\/i>parameters of the <b>Foreach-Object <\/b>cmdlet. I create a variable (<b>$aa<\/b>) and set its initial value to <b>$null<\/b>. I create and initialize the <b>$aa<\/b> variable in the <i>Begin <\/i>block, and the <i>Begin <\/i>block runs once at the beginning of the command. I now add the code that uses <b>[char]<\/b> to convert the random numbers into ASCII letters. In addition, I use the <b>+=<\/b> operator to store each letter back to the <b>$aa<\/b> variable.<\/p>\n<p>I place this code in the <i>Process <\/i>block where it will operate once for each item that comes across the pipeline (in this example, it runs twice because I am only choosing to generate two random numbers). If I left the code with only a B<i>egin <\/i>and a P<i>rocess <\/i>parameter, I would need to manually retrieve the value of the <b>$aa<\/b> variable. Here is the code as it currently stands:<\/p>\n<p style=\"padding-left: 30px\">Get-Random -Count 2 -InputObject (65..90) | % -begin {$aa=$null} -process {$aa += [char]$_}<\/p>\n<p>I do not want to manually retrieve the value of <b>$aa<\/b>. Instead, I want to automatically display the value that is contained in the variable. To do this, I add the <i>End <\/i>parameter. Inside the script block that is associated with the <i>End <\/i>parameter, I display the contents of the <b>$aa<\/b> variable. Here is the revised code with the added <i>End <\/i>parameter:<\/p>\n<p style=\"padding-left: 30px\">Get-Random -Count 2 -InputObject (65..90) | % -begin {$aa=$null} -process {$aa += [char]$_} -end {$aa}<\/p>\n<p>WooHoo! This is cool. I can add this code, and use it to create a sort of guess-the-letter game. I already wrote the engine to do this when I wrote the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/13\/add-random-question-features-to-a-powershell-game.aspx\" target=\"_blank\">Windows PowerShell cmdlet quiz<\/a>.<\/p>\n<p>The command that chooses two random numbers converts the numbers to their ASCII value and displays the results that are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1781.hsg-1-7-12-3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1781.hsg-1-7-12-3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is about all there is to choosing a couple of random numbers and using their ASCII value to convert them to letters. Join me tomorrow for more cool stuff using Windows PowerShell. It will be fun.<\/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><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use the Windows PowerShell Get-Random cmdlet to choose a specific number of random letters. Microsoft Scripting Guy, Ed Wilson, is here. With the advent of the New Year, I am hard at work on the 2012 Scripting Games. This year there will be several improvements. I [&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":[3,4,61,45,77],"class_list":["post-11551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use the Windows PowerShell Get-Random cmdlet to choose a specific number of random letters. Microsoft Scripting Guy, Ed Wilson, is here. With the advent of the New Year, I am hard at work on the 2012 Scripting Games. This year there will be several improvements. I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11551","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=11551"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11551\/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=11551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}