Find an unused drive letter


Your task is to write the shortest code to find one of the unused drive letters (excluding a,b and c). PowerShell’s default aliases are allowed. You have one week.  Answers should be posted as a comment to this brain teaser. That’s it.

The winner gets a copy of  the “VMware vSphere PowerCLI Reference” from Sybex!

We’ll post clues over the next couple days, stay tuned.

Good Luck!


We have a winner: P!

Your code was 36 characters long:


This is the breakdown of our solution (47 characters):

ls function:[d-z]: -n|?{!(test-path $_)} | random

We took a different approach (though more lengthy) to get a list of available by listing the default A-Z functions and using the Name switch to get the names only.

We then tested if a drive with the incoming drive letter name was available, if so, it was written back to the pipeline.

Finally, we used the Get-Random cmdlet to choose for us an available letter. As you can see we called Get-Random by its verb only. You may suspect that ‘random’ is an alias for that command but it isn’t. We used a known trick to shorten Get-Random – when PowerShell cannot resolve a command it tries to resolve it again by prepending the Get verb.

We hope you had fun and that you enjoyed participating and was able to learn a thing or two. See you in the next brain teaser.

We also would like to thanks our sponsors, Sybex, for giving away the book for the Winner.

Share on:
comments powered by Disqus