Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell and ASCII characters to automatically create folders with letters in the name.
Microsoft Scripting Guy, Ed Wilson, is here. I recently had a discussion with June and Kim, two of the other Windows PowerShell writers on my team, about creating some training for writers on other teams. Instead of focusing on things that are of general interest to IT Pros—things such as creating users in Active Directory, finding locked out users, or detailing group memberships for specific users—we decided that the Windows PowerShell examples should be of immediate practical use to the writers.
If writers spend several hours from their busy days to learn Windows PowerShell, they should be able to make efficiency gains to not only recoup the hours spent in training, but to receive additional free time as well. To a large extent, it is up to individual writers to see how they can improve their jobs and to find productivity boosts. After all, each writer’s style is different, and each writer’s method of work is self-inspired. As for me, I use lots of folders to organize my projects into manageable pieces. In the past, this meant spending a great deal of time creating folders—now I use Windows PowerShell to do the work.
Automatically creating folders
Using Windows PowerShell to create folders is not difficult. It can be as easy as piping an array of numbers to a Foreach-Object cmdlet that calls the New-Item cmdlet to create the folders. In the following command, I use the range operator to create an array of numbers from 1 through 20. I pipe the numbers to the Foreach-Object cmdlet (% is the alias). Inside the script block of the Foreach-Object cmdlet, I use the md alias to create a new directory. The alias md refers to the mkdir function. The mkdir function wraps the New-Item cmdlet to make it easier to create new directories. I rely on the expanding string (double quotation marks) to create the twenty folders. Here is the code that I use to create my twenty folders.
1..20 | % {md "Chapter$_"}
The command to create twenty folders, and the output associated with that command is shown in the following image.
Automatically creating folders with letters
As we have just seen, creating a collection of folders with numbers in the name is really easy. But what if I need to create a bunch of folders that use letters instead? For example, a book typically has Appendix A and not Appendix 1. In the past, creating various folders for the appendix was a pain (not the least of which was properly spelling appendix). Then one day, it dawned on me…I can use ASCII characters.
We need to first explore ASCII characters in Windows PowerShell…
Create an ASCII table
A long time ago (and I mean like printing with a nine-pin, dot-matrix printer on green-bar, tractor-fed paper), I used to be very good with the ASCII character codes. Today, I seldom use them. In the good old days, I had an ASCII table pinned on the wall in my office. Today, I would have to do a BING search to find a decent ASCII table—if it were not for Windows PowerShell. By using my range operator favorite trick, printing an ASCII table is very simple. In the code that follows, I use the range operator to create the numbers 0 through 127. I use the Foreach-Object (% is the alias) to use each number that comes across the pipeline. Inside the script block for the Foreach-Object cmdlet, I use parameter substitution, the –f operator to display the number, and the ASCII character value associated with each number. The [char] class converts the number to ASCII.
0..127 | % {“{0} {1}” –f $_, [char]$_}
The image that follows displays some of the output from the previous command.
Create folders with letters by using ASCII
Now that I know the ASCII values for letters, it is a simple matter to revisit the original code that created the folders. This time, instead of starting the range operator at 1, I will begin at 65. In the code that follows, I create numbers ranging from 65 through 70. I pipe the number to the Foreach-Object cmdlet (% is the alias). Inside the script block for the Foreach-Object cmdlet, I use the md alias (md is the alias for the mkdir function) to create directories. I build up the name by using Appendix as the prefix for each new directory. I use parameter substitution to add the letters A through F to the end of each folder name. The value that I substitute uses the [char] class to convert a number to an ASCII character. The command is shown here.
65..70 | % {md ("Appendix{0}" -f [char]$_)}
The command to create folders with letter names, along with the associated output are shown in the following image.
The technique of combining the range operator and ASCII values is not limited to automatically creating folders with letter names. I hope you will tuck this trick into your stocking of Windows PowerShell goodies. Join me tomorrow when Microsoft MVP, Sean Kearney, shifts gears by kicking off a week of Windows PowerShell and MDT. It is a great series of blog posts; you do not want to miss it. See ya.
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