0

I want to script some file operations on a particular external drive, that MUST NOT happen on any other drive (backups). The drive letter isn't a reliable identifier, so I found the volume ID and an UNC path referencing the drive using this volume ID through mountvol:

\\?\Volume{77013d6b-100000} (truncated for clarity)

While I am able to access a subdirectory using

Set-Location -LiteralPath \\?\Volume{77013d6b-100000}\path-that-exists

I cannot simply go to the disk's root:

Set-Location -LiteralPath \\?\Volume{77013d6b-100000} says that it cannot find the path because it does not exist.

How can I navigate directly to the external hard disk's root using only its volume ID?

bluppfisk
  • 467

2 Answers2

1

It's a bit awkward because the path without a folder name is just the volume ID, and powershell's filesystem provider doesn't correctly resolve it to a location. You can use the volume ID to find its current mount letter though like so:

# Find the unique ID for first time setup
# looks like: `\\?\Volume{00000000-0000-0000-0000-000000000000}\`
Get-Volume | Select DriveLetter,UniqueID

in your script, find the current mount point of that volume

$vol = Get-Volume -UniqueID '\?\Volume{00000000-0000-0000-0000-000000000000}'

navigate to the root

Set-Location "$($vol.DriveLetter):"


The GUID won't change if the OS stays the same, but if you plan to use that drive and script on another PC for example, you'll need to start with properties of the physical disk. I recommend using serial number (if present), but name or size can work fine as well:

# get drive letter by SN. Note: assumes only one mounted volume on external drive
$vol = Get-Disk -SerialNumber $sn | Get-Partition | Get-Volume | Where DriveLetter

navigate to the root

Set-Location "$($vol.DriveLetter):"

Cpt.Whale
  • 10,914
1

Another method:

  • By VolumeName/ Description
Set-Location (PSDrive | ? Description -eq 'Name of..').Root
Set-Location (Get-WMIObject Win32_LogicalDisk| ? VolumeName -eq 'Name of..').DeviceID
  • By VolumeSerialNumber
CD (Get-WMIObject Win32_LogicalDisk| ? VolumeSerialNumber -eq 'Sn of..').DeviceID

To Check SN (GWMI Win32_LogicalDisk).VolumeSerialNumber
To Check Description (PSDrive).Description
Get-WMIObject a.k.a gwmi in PS7 sometimes as Get-CimInstance

Mr.Key7
  • 957