title: Powershell check if drive letter exists and if so remove
body: <p>I have a script that checks whether there are drives with certain letters.
  If that letter exists, it will removes these letter.
  Then the script goes looking for drives according to their 'labels' in changes their letters according to what is specified in the script.</p>
  
  <p><strong>I'm looking to perfect the script so that in the first part it will look for any drives with the letters E or D, and if so, remove them.
  If it is not found, then just go to the second part of changing according to the label of the disk.</strong></p>
  
  <pre><code>Get-Volume -DriveLetter E | Get-Partition | Remove-PartitionAccessPath -AccessPath E:\
  Get-Volume -DriveLetter D | Get-Partition | Remove-PartitionAccessPath -AccessPath D:\
  
  $DataPartition = Get-WMIObject Win32_Volume | where{ $_.Label -eq 'Data'}
  $FileServerPartition = Get-WMIObject Win32_Volume | where{ $_.Label -eq 'FileServer'}
  
  $DataPartition.DriveLetter = $null
  $DataPartition.Put()
  $FileServerPartition.DriveLetter = $null
  $FileServerPartition.Put()
  
  
  Try
  {
      Set-WmiInstance -input $DataPartition -Arguments @{DriveLetter="D:"} | Out-File -FilePath  C:\Windows\Temp\FixPartitionsLog.txt -Append
      Set-WmiInstance -input $FileServerPartition -Arguments @{DriveLetter="E:"} | Out-File -FilePath  C:\Windows\Temp\FixPartitionsLog.txt -Append
  }
  Catch
  {
      $ErrorMessage = $_.Exception.Message | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt
  }
  sleep 5
  Restart-Service server -Force
  </code></pre>