Use PowerShell and ASCII to Create Folders with Letters

Use PowerShell and ASCII to Create Folders with Letters

  • Comments 7
  • Likes

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.

Image of command output

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.

Image of command output

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.

Image of command output

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    merry christmas to all Scripting guys and all readers of this blog!

    Well Ed, this blog article is something, we can use in every day's work.

    And still I'm a little bit "sad" that we can't use characters as indices in loops!

    It would be nice to have a loop like:

    'A'..'F' | % {md "Appendix$_"}

    Maybe next time ? ( version 3.x ? )

    Klaus.

  • @Klaus

    What ... no characters inyou machine.  MIne has lots of characters to generate.

    [int]'a'[0]..[int]'z'[0]|%{[char]$_}

    Work just fine.

  • Did you know that we can also do math with strings?

    'a'*10

    "Happy New Year!`n" * 20

  • Why all the extra type conversion?  This works too...

    'A'[0]..'Z'[0] | % {md "Appendix$_"}

  • @Klaus Schulte I am glad you can use this blog article every day at work ... it is something I use on a regular basis as well, and is why I decided to write the article. I really wish I could use a letter in a loop as well ... it would really make a lot of things much easier. I also hope you had a great holiday.

  • @JV what @Klaus and I were talking about was using string values directly in a loop -- which cannot be done. Your approach, however, is really good, and is a great work around ... the advantage of your approach, is that you take the letters themself, obtain the ascii values, and then change them back to the letter later on ... while my approach starts with numbers that will represent the ascii values, your solution is more readable.

    @JV yes, I know you can multiply strings ... it is way cool. I use this approach to create an underline that is exactly the same length as a particular string ... from time to time.

  • @Bob The reason for the type conversion, is to be able to create folders with the name of AppendixA, AppendixB etc. Not Appendix1, Appendix2. For example, Microsoft Press names each Appendix with a letter, and not a number -- so having a letter in the folder name, instead of a number was a requirement of the scenario. Your solution, while perfectly valid, and definately simplier, does not create folders with letters, but rather folders with numbers.