WELCOME TO BLOG-STACK.NET

A collection of technical solutions from all corners of IT

Homepage

The Application Stack

Posts related to Microsoft software

Latest Posts on Microsoft products

The Networking Stack

All posts related to IP routing and switching

Latest Networking Posts

The Virtualization Stack

VMWare and Hyper-V related posts

info@website.com

+123 456 7890 | +123 456 7890

Automate the Stack!

Everything related to scripting, programming, and making life easier!

View Posts on Automation and Scripting

Powershell next available drive letter

for($j=67;gdr($d=[char]++$j)2>0){}$d That’s it!

psdriveletter

This example was posted on powershellmagazine and was the winning entry for the shortest code (36 chars) that lists the next available drive letter. Note that the above one-liner returns the drive letter without the colon.

If you are creating a script that requires the colon, you can simply tack on +’:’ to the end of the command:

for($j=67;gdr($d=[char]++$j)2>0){}$d+':'

psdriveletter2

Another example that I have modified from powershellmagazine that returns the next available drive letter including the colon is:

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

psdriveletter3___________________________________________________________________________________________

Now let’s breakdown the first one-liner and explain what’s going on.

1. $j=67 <-67 is used because the character C is 67 in ASCII (example: the command ‘[char]67’ will return C)
2. gdr is an alias for Get-PSDrive which lists drive letters in use. gdr([char]67) will return info about the C: drive
3. $d=[char]++$j <-This will evaluate to “D” during the first iteration because $j=67 and ++$j says to add 1 to 67 (command ‘[char]68’ returns D)
4. 2>0 <-Send errors to a file named “0”
5.$d <- Print out the value of $d after the For Loop exits

The For Loop actually exits in spectacular fashion; it blows up, and this “functionality” is actually necessary for it to return the next unused drive letter. As long as the gdr command returns info about the drive, the loop will continue. Once it attempts to return info about a drive not in use, it throws an error and redirects the output to a file (“0” in this example) in the current working directory while returning to the console what $d equaled when attempting to run the gdr command.

NOTE: You can change the requirement of outputting the error to a file by outputting to a variable. This is accomplished by changing 2>0 to 2>$0
for($j=67;gdr($d=[char]++$j)2>$0){}$d

___________________________________________________________________________________________

Examples:

Map a network drive with New-PSDrive:

New-PSDrive -Name $(for($j=67;gdr($d=[char]$j++)2>0){}$d) -PSProvider FileSystem -Root \\server\share

Map a network drive with net use:
net use (ls function:[d-z]: -n | ?{ !(test-path $_) })[0] \\server\share
___________________________________________________________________________________________

One thought on “Powershell next available drive letter

  1. Couldn’t find any way of making this work remotely so I wrote my own… this will put the next available drive letter into $driveletter for RemoteHost

    67..90 | foreach {if(((GWmi win32_logicaldisk -computer RemoteHost -Property DeviceID).deviceID).Substring(0,1) -notcontains [char]$_){$driveLetter = [char]$_; break}}

Leave a Reply

Your email address will not be published. Required fields are marked *