Author Posts

April 2, 2014 at 12:11 pm

Add-Type @"
using System;
using System.Runtime.InteropServices;

public class MountPoint
{
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool DeleteVolumeMountPoint(string mountPoint);
}
"@

[MountPoint]::DeleteVolumeMountPoint("G:\")

Is it possible to do it more simple, for example with WMI?
Sorry, I'm new in PowerShell

April 2, 2014 at 9:02 pm

In Win8/2012 and newer OSs, yes.
Remove-PSDrive

April 3, 2014 at 3:46 am

Sorry, but it is not a solution. I need to remove Drive Letter from Physical Drive.

Remove-PSDrive cannot delete Windows physical or logical drives. It works only with mapped network drives.

April 3, 2014 at 5:35 am

This seems to work:

$target = 'G:'
$volume = Get-WmiObject Win32_Volume -Filter "DriveLetter='$target'"

if ($null -ne $volume)
{
    $volume.DriveLetter = $null
    $volume.Put()
}

I haven't figured out a way to make that work with Set-WmiInstance yet (assigning $null to a property doesn't appear to do anything).

April 3, 2014 at 5:57 pm

Sorry. So you have an existing partition on a physical drive that you want to no longer have a drive letter assigned? Or remove the partition? Or just hide the partition?

Set-Partition has a parameter of NoDefaultDriveLetter that may be what you are looking for, but I would need more specifics.

April 4, 2014 at 5:22 am

Thank you, Dave. May be it is not in pure powershell, but it works! I appreciate.

Steve, yeah I don't want to longer have a drive letter assigned, but do not remove drive. I don't want to hide partition, of course I can do it (in user space) from registry.

From Help:
-NoDefaultDriveLetter
Specifies that the automatic assignment of a drive letter is disabled. This is only honored for GPT disks. This attribute is useful in storage area network (SAN) environments.

So, Set-Partition is a wrong way.

April 4, 2014 at 6:50 am

You can simplify

$target = 'G:'
if (Get-WmiObject Win32_Volume -Filter "DriveLetter='$target'")
{
$volume.DriveLetter = $null
$volume.Put()
}