How to remove Drive Letter in PowerShell way

This topic contains 6 replies, has 4 voices, and was last updated by  Richard Siddaway 3 years, 4 months ago.

  • Author
    Posts
  • #14274

    Ildar
    Participant

    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

  • #14288

    Steve Mahoney
    Participant

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

  • #14296

    Ildar
    Participant

    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.

  • #14316

    Steve Mahoney
    Participant

    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.

  • #14318

    Ildar
    Participant

    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.

  • #14325

    Richard Siddaway
    Moderator

    You can simplify

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

  • #14297

    Dave Wyatt
    Moderator

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

You must be logged in to reply to this topic.