{"id":2167,"date":"2014-01-22T00:01:00","date_gmt":"2014-01-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/22\/use-powershell-to-find-two-letter-alias-combinations\/"},"modified":"2014-01-22T00:01:00","modified_gmt":"2014-01-22T00:01:00","slug":"use-powershell-to-find-two-letter-alias-combinations","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-find-two-letter-alias-combinations\/","title":{"rendered":"Use PowerShell to Find Two-Letter Alias Combinations"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about finding all two-letter alias combinations by using Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-Best-Practices-Wilson\/dp\/0735666490\/ref=sr_1_2?ie=UTF8&amp;qid=1386004821&amp;sr=8-2&amp;keywords=powershell+best+practices\">PowerShell Best Practices<\/a>, which is published by Microsoft Press.<\/p>\n<p class=\"SbarParafirst\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0246.bestpracticebookcover.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0246.bestpracticebookcover.jpg\" alt=\"Image of book\" title=\"Image of book\" \/><\/a><\/p>\n<p class=\"SbarParafirst\">The two-letter alias namespace is rather large, but how large is it really? You must take every letter in the a&ndash;z range and pair them with every other letter in the a&ndash;z range to get the answer. If you are good with math, you already know that there are 676 possible letter combinations. However, if your math skills are a bit rusty (or just for fun), you can write a Windows PowerShell script to figure out the answer.<\/p>\n<p class=\"SbarParafirst\">The problem with this approach is that you cannot use the <b>range<\/b> operator (<b>..<\/b>) to produce a range of letters. The <b>range<\/b> operator works with numbers 1..10, automatically creates a range of numbers with the values 1 through 10, and can save you a great deal of typing. However, because you have ASCII numeric representations of the letters a&ndash;z, you can use the <b>range<\/b> operator to create a range of the letters.<\/p>\n<p class=\"SbarParafirst\">The ASCII value 97 is the a character, and ASCII 122 is z. When you determine the numeric range, you can use the <b>ForEach-Object<\/b> cmdlet and convert each letter to a character by using the <b>[char]<\/b> type. You can store the resulting array of letters in the <b>$letters<\/b> variable.<\/p>\n<p class=\"SbarParafirst\">After performing two loops through the array, you can store the resulting letter combinations in the <b>$lettercombination<\/b> variable, which is constrained as an array by using the <b>[array]<\/b> type. The <b>Measure-Object<\/b> cmdlet is used to count the number of possible letter combinations. GetTwoLetterAliasCombinations.ps1 script is shown here.<\/p>\n<p class=\"SbarCodeBlockScreened\"><strong>GetTwoLetterAliasCombinations.ps1<\/strong><\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">$letterCombinations = $null<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">$asciiNum = 97..122<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">$letters = $asciiNum | ForEach-Object { [char]$_ }<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">Foreach ($1letter in $letters)<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">{<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">&nbsp;Foreach ($2letter in $letters)<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">&nbsp;{[array]$letterCombinations += &quot;$1letter$2letter&quot;} }<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">&quot;There are &quot; + ($letterCombinations | Measure-Object).count +<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">&quot; possible combinations&quot;<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">&quot;They are listed here: &quot;<\/p>\n<p class=\"SbarCodeBlockScreened\" style=\"margin-left:30px\">$letterCombinations<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about finding all two-letter alias combinations by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new book, PowerShell Best Practices, which is published by Microsoft Press. The two-letter alias namespace is rather large, but how large is it really? [&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":[331,460,3,4,45],"class_list":["post-2167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-best-practices","tag-powershell-4","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about finding all two-letter alias combinations by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new book, PowerShell Best Practices, which is published by Microsoft Press. The two-letter alias namespace is rather large, but how large is it really? [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2167","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=2167"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2167\/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=2167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}