Home > Net >  How to remove partition access path without knowing the path of VHD
How to remove partition access path without knowing the path of VHD

Time:08-17

I used Powershell Add-PartitionAccessPath to map a partition in a VHDX file to say c:\temp\mysupport folder.

I am trying to create a function that takes access path as input parameter and remove it. I see Remove-PartitionAccessPath is available. But it expects you to pass disk number, partition number etc.

Wondering if there is a way to map a folder mount point back to partition object, disk objects etc to remove it.

CodePudding user response:

Little bit of googling, I found a way to do it. It is not that straight forward. The blog article Locating Mount points helped me come up with this snippet.

Below is code snippet:

# Make sure the path is absolute path ending with \. 
# If the path doesn't end \, the filter logic
# on Get-WmiObject won't work
$dirMount="$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($dirMount))\"
$aVol = Get-WmiObject Win32_Volume | Where-Object -FilterScript {$_.Caption -eq "$dirMount"}
if (-not $aVol)
{
    echo "ERROR: [$($MyInvocation.MyCommand)] Unable to map $dirMount to a volume"
    return $false
}
$volID = $aVol.DeviceID
if ($volID)
{
    $aVol = Get-Volume -UniqueId "$volID" |Get-Partition|Remove-PartitionAccessPath -AccessPath "$dirMount"
    $result = ($aVol -eq $null)
}
  • Related