Getting a free drive letter

后端 未结 11 583
青春惊慌失措
青春惊慌失措 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 14:16

    $taken = Get-WmiObject Win32_LogicalDisk | Select -expand DeviceID
    $letter = 65..90 | ForEach-Object{ [char]$_ + ":" }
    (Compare-Object -ReferenceObject $letter -DifferenceObject $taken)[1].InputObject
    

    Just for fun to shave an extra line of code (lol). If you wanted to be cloppy as heck you could skip instantiating variables and just pipe those directly into -Ref and -Diff directly, probably ought to be slapped for doing that though. :)

    Selects [1] to avoid getting the A: drive just in case that might complicate matters.

提交回复
热议问题