Getting a free drive letter

后端 未结 11 576
青春惊慌失措
青春惊慌失措 2020-12-10 13:40

I saw the Get-NextFreeDrive function in this answer and I wondered if there was a more efficient way to do this. It appears that the function in the linked answ

11条回答
  •  臣服心动
    2020-12-10 13:57

    I found to my own cost that the currently accepted answer (ls function:[d-z]: -n | ?{ !(test-path $_) } | random) can indeed return things like CD drives.

    I've made this one to exclude any local drives from the array:

    "$([char[]]([char]'D'..[char]'Z')|Where-Object {((Get-WmiObject -Class Win32_LogicalDisk).DeviceID).replace(':','') -notcontains $_ }|Select-Object -first 1):"
    

    It will return the first available letter. If you'd prefer the last available letter just change Select-Object -first 1 to Select-Object -last 1

提交回复
热议问题