PS Script to Map a Drive with Free Drive Letter in Descending Order

In various cases, you’d like to have a floating drive (created by subst) created with a XenApp User session and you’d need to overcome the drive letter conflict while setting up this.  The easier option is to start checking the drive availability in the descending order and map the drive to a free letter. The preferable option is to have this done at the end of a user logon script so that you’ll get to the free drive letter to be used. Here is a quick PS code that can help you achieve this.

PS Script:

foreach ($letter in [char[]]([char]’Z’..[char]’A’)) {
    $drive = $letter + ‘:’
    if (!(Test-Path -path $drive)){
        break
    }
}
Write-Host "$drive is free and can be used"

 

Output:

Z: is free and can be used

Leave a Reply

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