PowerShell Function to Determine Available Drive Letters

I’ve recently been working on some file server drive migrations. One of the steps I needed to perform was to change the drive letter of a current drive to a different available drive letter so I decided to write a PowerShell function to accomplish the task of determining what drive letters are available.

The Get-MrAvailableDriveLetter function shown in the following code example will run on systems with PowerShell version 2.0 or higher (I tested it all the way down to version 1.0, but that was a no go).

It contains a requires statement and comment based help as all well written functions that you plan to share should:

Drive letters can be excluded via parameter input and by default A-F and Z are excluded. This can be negated by specifying $null or an empty string for the ExcludeDriveLetter parameter:

By default only the first available drive letter is returned and it can be randomized by specifying the Random parameter:

All available drive letters can be returned by specifying the All parameter and they can also be returned in random order with the Random parameter:

The drive letters themselves are obtained from the Function PSDrive:

The excluded drive letters are converted to a regular expression to minimize the amount of code that had to be written to exclude multiple drive letters and/or ranges of drive letters:

If the Random parameter is specified, all of the available drive letters are randomized once the exclusions are removed. This allows a single line of code to be used regardless if a single or all results are desired to be returned in randomized order:

Unless the All parameter is specified, the Return keyword is used to terminate execution once an available drive letter is found and tested to not be in use. Read more about the PowerShell Return keyword in a previous blog article that I’ve written.

If the All parameter is specified, all drive letters are tested and returned if they are not in use. This seemed to be the most efficient way to accomplish this task without testing all of them early on only to return a single result by default:

The function itself is simple to use:

availabledriveletter1a

The Get-MrAvailableDriveLetter function shown in this blog article can be downloaded from my PowerShell repository on GitHub.

µ

Leave a Reply

%d bloggers like this: