Getting a free drive letter

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

    I found out that Test-Path evaluates my empty CD-Drive as False, here is another alternative that will compare every letter in the alphabeth until it finds one that doesn't exist in filesystem, then returns that drive as output.

    $DriveLetter = [int][char]'C'
    WHILE((Get-PSDrive -PSProvider filesystem).Name -contains [char]$DriveLetter){$DriveLetter++}
    Write-Host "$([char]$Driveletter):"
    

提交回复
热议问题