Getting a free drive letter

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

    I like this way, for the following reasons:

    1. It doesn't require WMI, just regular powershell cmdlets
    2. It is very clear and easy to read
    3. It easily allows you to exclude specific driveletters
    4. It easily allows you to order the driveletters in any order you would like
    5. It finds the first non used driveletter and maps it, and then it is finished.

      $share="\\Server\Share"
      $drvlist=(Get-PSDrive -PSProvider filesystem).Name
      Foreach ($drvletter in "DEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()) {
          If ($drvlist -notcontains $drvletter) {
              $drv=New-PSDrive -PSProvider filesystem -Name $drvletter -Root $share
              break
          }
      }
      

提交回复
热议问题