PowerTip: Use PowerShell to Remove Drive Letter


Summary: Learn how to use Windows PowerShell to remove an assigned drive letter from a partition.

Hey, Scripting Guy! Question How can I use Windows PowerShell to remove a drive letter from a partition?

Hey, Scripting Guy! Answer Use a combination of three cmdlets with the drive letter—for example, to remove drive letter X from the drive
           it is assigned to, use

$DriveLetter=’X
Get-Volume -Drive $DriveLetter | Get-Partition | Remove-PartitionAccessPath
-accesspath "$DriveLetter`:\"

Comments (4)

  1. B Josephs says:

    Thanks for the tip – but I couldn’t get it to work. I also tried hard-coding the drive letter without success.

  2. @B Josephs

    I think I see a typo. That first line should show

    $DriveLetter=’X’ ; # <- There was a quote missing in this line

    If you’d like, if you download the DeployModule from PowerShellGallery.com this is wrapped in a "Remove-DriveLetter" function to help you along

    Cheers!
    Sean

  3. Thanks! But unfortunately this does not work with optical (CD/DVD/BD) drives 🙁

  4. Dan Donovan says:

    Hey guys,

    I’m new to scripting with Powershell and I could not get this working either. I’d planned on using it as a basis for removing an unwanted drive letter. With that in mind, I’d like to present my solution which will remove a drive based on it’s volume label. I’ve used the below script (run as admin) on Windows 8 and it works perfectly:

    $DriveLetter = (Get-Volume -FileSystemLabel “SYSTEM RESERVED”).DriveLetter
    $AccPath = ($DriveLetter + “:”)
    $PartNo = (Get-Partition -DriveLetter $DriveLetter).PartitionNumber
    $DiskNo = (Get-Partition -DriveLetter $DriveLetter).DiskNumber

    Remove-PartitionAccessPath -DiskNumber $DiskNo -PartitionNumber $PartNo -AccessPath $AccPath

    We’ve used this as part of our build process (which suddenly seems to assign a random dive letter to the SYSTEM RESERVED partition).

    Hope this solution is useful to someone else

    Kind regards
    Dan

Skip to main content