Share via


Removing drive letter using Powershell

Question

Friday, March 2, 2018 10:59 PM

Hi guys,

Newbie here with Powershell, just to let you know!

I am trying to remove a drive letter using powershell using the below code (File name: RemoveDL.ps1):

$drive = gwmi win32_volume -Filter "DriveLetter = 'Q:'"
$drive.DriveLetter = ":"
$drive.put() 

I have also created a batch file to run this with the code as follows (File name: RunRemoveDL.bat):

powershell "C:\Users\dan\desktop\RemoveDL.ps1"

I have then created a shortcut of the file RunRemoveDL.bat and then went to Properties > Shortcut > Advanced and ticked 'Run as administrator'.

However the issue I am having is that, when I running  it is still giving an access denied message. Whole error message shown below:

Exception calling "Put" with "0" argument(s): "Access is denied.
"
At C:\Users\Dan\Desktop\RemoveDL.ps1:3 char:1
+ $drive.put()
+ ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

So my two questions about this are:

1) How can I run the powershell script as administrator without right clicking and selecting 'Run as administrator'?

2) Is this the best way to remove a drive letter or is there a better script/(s) I can use?

Thanks,

Dan Goodwin

All replies (12)

Friday, March 2, 2018 11:04 PM

Use:

Remove-PsDrive -Name C -Force

To run elevated:

Start-Process powershell -Argument "-file yourfile.ps1" -Verb RunAs

\(ツ)_/


Friday, March 2, 2018 11:10 PM

Thanks for that jrv :) 

I have tried the code you advised to use and I think powershell is expecting a follow-up command to be run.

Powershell is just showing the following:

https://imgur.com/a/4XRPY


Friday, March 2, 2018 11:13 PM

No.  The drive has been removed.  If you try the same command again you will get an error.

\(ツ)_/


Friday, March 2, 2018 11:28 PM

Drive Q is still there and can be accessed.


Friday, March 2, 2018 11:30 PM

You cannot remove physical drives.  USB drives have to be ejected to remove them.

\(ツ)_/


Friday, March 2, 2018 11:38 PM

it is a drive on my SSD. See image in link

https://imgur.com/a/a3AI3

The way I would do it in cmd is

diskpart

select volume 3

remove

exit

I want to replicate these events in powershell, however cannot find a code that will make remove drive Q


Friday, March 2, 2018 11:43 PM

You cannot remove drives that are physically on the system.  You can go to disk manager and remove the drive letter if you wish.  If files are open on the drive this will be difficult.

You can also use "diskpart" to remove drive letters.

\(ツ)_/


Friday, March 2, 2018 11:45 PM

so in powershell, there is no code that will let you unassign a partition letter? 

So with my Q drive. I want to remove the drive letter but still have the partition there.

I hope that makes sense.


Friday, March 2, 2018 11:48 PM

to remove a drive letter in COMMAND PROMPT I do the below:

dispart

select volume 3

remove

exit

I can then assign this letter back using the following:

diskpart

select volume 3

assign letter=Q

exit

Saturday, March 3, 2018 12:03 AM

so in powershell, there is no code that will let you unassign a partition letter? 

So with my Q drive. I want to remove the drive letter but still have the partition there.

I hope that makes sense.

Actually there is:

Get-Volume -Drive Q | Get-Partition | Remove-PartitionAccessPath -accesspath Q:\

\(ツ)_/


Friday, September 28, 2018 4:24 AM | 1 vote

Thanks, that worked.

Modified it a bit because I was looking to remove specific drive letters with labels like "HP_RECOVERY" and "Recovery Image". 

$DL = (Get-Volume | Where-Object {$_.FileSystemLabel -eq "HP_RECOVERY"} | Select -ExpandProperty DriveLetter)+":"

Get-Partition | Where-Object {$_.DriveLetter -match $DL.Substring(0,1)} | Remove-PartitionAccessPath -AccessPath $DL


Wednesday, July 24, 2019 7:15 PM

Thanks, that worked.

Modified it a bit because I was looking to remove specific drive letters with labels like "HP_RECOVERY" and "Recovery Image". 

$DL = (Get-Volume | Where-Object {$_.FileSystemLabel -eq "HP_RECOVERY"} | Select -ExpandProperty DriveLetter)+":"

Get-Partition | Where-Object {$_.DriveLetter -match $DL.Substring(0,1)} | Remove-PartitionAccessPath -AccessPath $DL

This was very helpful to myself and our team!  Thank you!