How to remove Drive Letter in PowerShell way

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);

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

In Win8/2012 and newer OSs, yes.

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.

This seems to work:

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

if ($null -ne $volume)
    $volume.DriveLetter = $null

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).

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.

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:
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.

You can simplify

$target = ‘G:’
if (Get-WmiObject Win32_Volume -Filter “DriveLetter=’$target’”)
$volume.DriveLetter = $null