Getting a free drive letter

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

    Here's what I came up with. I need the last available drive letter from A to Z.

    $AllLetters = 65..90 | ForEach-Object {[char]$_ + ":"}
    $UsedLetters = get-wmiobject win32_logicaldisk | select -expand deviceid
    $FreeLetters = $AllLetters | Where-Object {$UsedLetters -notcontains $_}
    $FreeLetters | select-object -last 1
    
    • This gets an array of letters A..Z
    • Then gets an array of the letters already in use from WMI
    • Next produces an array of letters not in use using the comparison operator -notcontains
    • Finally outputs a single letter.

提交回复
热议问题