DevBlogs
January 22nd, 2014

# Use PowerShell to Find Two-Letter Alias Combinations

Scripter

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? You must take every letter in the a–z range and pair them with every other letter in the a–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.

The problem with this approach is that you cannot use the range operator (..) to produce a range of letters. The range 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–z, you can use the range operator to create a range of the letters.

The ASCII value 97 is the a character, and ASCII 122 is z. When you determine the numeric range, you can use the ForEach-Object cmdlet and convert each letter to a character by using the [char] type. You can store the resulting array of letters in the \$letters variable.

After performing two loops through the array, you can store the resulting letter combinations in the \$lettercombination variable, which is constrained as an array by using the [array] type. The Measure-Object cmdlet is used to count the number of possible letter combinations. GetTwoLetterAliasCombinations.ps1 script is shown here.

GetTwoLetterAliasCombinations.ps1

\$letterCombinations = \$null

\$asciiNum = 97..122

\$letters = \$asciiNum | ForEach-Object { [char]\$_ }

Foreach (\$1letter in \$letters)

{

Foreach (\$2letter in \$letters)

{[array]\$letterCombinations += "\$1letter\$2letter"} }

"There are " + (\$letterCombinations | Measure-Object).count +

" possible combinations"

"They are listed here: "

\$letterCombinations

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

## Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.