none
Remove Drive Letter RRS feed

  • Question

  • I'm working on a powershell script to automate the refresh of testing environment and have run into a problem.

    I need to remove the drive letter for a drive.  Currently the drive is grabbing the drive letter automatically when it comes online.  The drive is snapshot from our SAN.  I have found several different ways to change the drive letter, but nothing about how to remove the drive letter.

    I will be using the drive as a pass-through disk with a Hyper-V VM.  I don't want a drive letter assigned as I would eventually run out of letters.

     

    Hopefully someone knows how to remove the drive letter.

     

    Thanks

    Thursday, January 12, 2012 10:30 PM

Answers

  • Thanks for the replies, but i found out how to do it in powershell using the WMI object.
    You simply set the drive letter to null and that will remove the drive letter.

     

    Here is what i came up with:

                        $private:diskNumber = $diskItem.DeviceName
                        $private:diskNumber = $diskNumber.Replace("\\?\PhysicalDrive", "")
                        $private:wmiDisk = Get-WMIObject Win32_DiskPartition -computername $VMtoRefresh.HostName -filter "DiskIndex=$disknumber"
                        # get the drive letter by finding the related Win32_LogicalDisk 
                        $private:wmidriveLetter =  $wmidisk.psbase.GetRelated('Win32_LogicalDisk') | Select-Object -ExpandProperty DeviceID 
                        Write-Host "Removing the drive letter, $wmiDriveLetter, from " $diskItem.DeviceName " ..."
                        $private:drive = Get-WmiObject -Class Win32_volume -Filter "DriveLetter='$wmidriveletter'" -computername $VMtoRefresh.HostName
                        if ($drive -ne $null) {Set-WmiInstance -input $drive -Arguments @{DriveLetter=$null;Label=$newReplayName} | Out-Null}
                        $private:diskNumber = $diskItem.DeviceName

    • Marked as answer by Chas SB Tuesday, January 17, 2012 2:10 PM
    Tuesday, January 17, 2012 2:10 PM

All replies

  • Go into disk management and remove the drive letter.

    Your SAN software is what is assigning the letter so look at teh configuration of the SAN subsystem.

     


    jv
    Thursday, January 12, 2012 11:10 PM
  • Try mountvol.exe  (ex. mountvol Q: /d)

    http://ss64.com/nt/mountvol.html


    Gastone Canali >http://www.armadillo.it
    Friday, January 13, 2012 12:36 AM
  • Thanks for the replies, but i found out how to do it in powershell using the WMI object.
    You simply set the drive letter to null and that will remove the drive letter.

     

    Here is what i came up with:

                        $private:diskNumber = $diskItem.DeviceName
                        $private:diskNumber = $diskNumber.Replace("\\?\PhysicalDrive", "")
                        $private:wmiDisk = Get-WMIObject Win32_DiskPartition -computername $VMtoRefresh.HostName -filter "DiskIndex=$disknumber"
                        # get the drive letter by finding the related Win32_LogicalDisk 
                        $private:wmidriveLetter =  $wmidisk.psbase.GetRelated('Win32_LogicalDisk') | Select-Object -ExpandProperty DeviceID 
                        Write-Host "Removing the drive letter, $wmiDriveLetter, from " $diskItem.DeviceName " ..."
                        $private:drive = Get-WmiObject -Class Win32_volume -Filter "DriveLetter='$wmidriveletter'" -computername $VMtoRefresh.HostName
                        if ($drive -ne $null) {Set-WmiInstance -input $drive -Arguments @{DriveLetter=$null;Label=$newReplayName} | Out-Null}
                        $private:diskNumber = $diskItem.DeviceName

    • Marked as answer by Chas SB Tuesday, January 17, 2012 2:10 PM
    Tuesday, January 17, 2012 2:10 PM
  • Thanks for the replies, but i found out how to do it in powershell using the WMI object.
    You simply set the drive letter to null and that will remove the drive letter.

     

    Here is what i came up with:

                        $private:diskNumber = $diskItem.DeviceName
                        $private:diskNumber = $diskNumber.Replace("\\?\PhysicalDrive", "")
                        $private:wmiDisk = Get-WMIObject Win32_DiskPartition -computername $VMtoRefresh.HostName -filter "DiskIndex=$disknumber"
                        # get the drive letter by finding the related Win32_LogicalDisk 
                        $private:wmidriveLetter =  $wmidisk.psbase.GetRelated('Win32_LogicalDisk') | Select-Object -ExpandProperty DeviceID 
                        Write-Host "Removing the drive letter, $wmiDriveLetter, from " $diskItem.DeviceName " ..."
                        $private:drive = Get-WmiObject -Class Win32_volume -Filter "DriveLetter='$wmidriveletter'" -computername $VMtoRefresh.HostName
                        if ($drive -ne $null) {Set-WmiInstance -input $drive -Arguments @{DriveLetter=$null;Label=$newReplayName} | Out-Null}
                        $private:diskNumber = $diskItem.DeviceName


    That does not really remove the drive. Youi have hust removed the drive letter. 

    Try this for a dismount.  THis may work on a SAN but only under some conditions.

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa390368(v=vs.85).aspx

     

     

     


    jv
    Tuesday, January 17, 2012 2:50 PM
  • all i wanted to do is remove the drive letter.
    Tuesday, January 17, 2012 3:04 PM
  • all i wanted to do is remove the drive letter.


    What do you mean by removing the drive letter?

     


    jv
    Tuesday, January 17, 2012 3:12 PM
  • you can break this down to an even simpler script if you know the drive letter

    $drive = gwmi win32_volume -Filter "DriveLetter = 'd:'"
    Set-WmiInstance -input $drive -Arguments @{DriveLetter=$null;Label=$newReplayName}

    Thanks by the way I needed this for a similar solution and you put me on the right track!


    Jedi Hammond Dell Services

    Monday, September 23, 2013 9:53 PM
  • pure powershell:$v='F'; Get-Volume -DriveLetter $v | Remove-PartitionAccessPath -AccessPath "$($v):\"
    Wednesday, November 12, 2014 7:50 PM