How to remove Drive Letter in PowerShell way

Welcome Forums General PowerShell Q&A How to remove Drive Letter in PowerShell way

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
4 years, 11 months ago.

  • Author
    Posts
  • #14274

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

  • #14296

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    You can simplify

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

  • #14297

    Member
    Points: 0
    Rank: Member

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

The topic ‘How to remove Drive Letter in PowerShell way’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort