Access to a disk drive using volume ID instead of a drive letter in Windows
You can use mountvol
in the command prompt to get the ID of the volume you want to access.
This IDs can be used to open an explorer window, independent from the drive letter
To create a shortcut to the drive, create a new batch file with this content:
start \\?\Volume{1b3b1146-4076-11e1-84aa-806e6f6e6963}\
In PowerShell, use Get-Volume
piped through Format-List
like this:
get-volume | fl
will give you everything you need, for instance this SYSTEM RESERVED
volume on one of my machines:
ObjectId : {1}\\ACER-M3900\root/Microsoft/Windows/Storage/Providers_v2\WSP_Volume.ObjectId="{5b16a307-de54-11e7-8aeb-806e6f6e6963}:VO:\\?\Volume{b41b0670-0000-0000-00e8-0e8004000000}\"
PassThroughClass :
PassThroughIds :
PassThroughNamespace :
PassThroughServer :
UniqueId : \\?\Volume{b41b0670-0000-0000-00e8-0e8004000000}\
AllocationUnitSize : 4096
DedupMode : NotAvailable
DriveLetter :
DriveType : Fixed
FileSystem : NTFS
FileSystemLabel : SYSTEM RESERVED
FileSystemType : NTFS
HealthStatus : Healthy
OperationalStatus : OK
Path : \\?\Volume{b41b0670-0000-0000-00e8-0e8004000000}\
Size : 105058304
SizeRemaining : 33992704
PSComputerName :
I prefer to use absolute drive letters for flash drives. There are adequate assignments for just about anybody unless you have 20 or more flash drives.
In Windows 7 use Windows to format and assign a drive label or if the drive already has a label, use command line "Label (drive letter) to give it a label of your choice.
Then go to control panel, system and security, administrative tools, computer management, disk management to select the drive with the label you created by right clicking on the drive, select change drive letter and paths and assign the volume to a specific drive letter. It will always mount to that drive letter if it is available.
I have labeled drives that contained live data and the labeling did not affect the existing data in any way. It seems to work just like it did on floppies back in the good old days
Using a Label
You can access a disk drive using its Label in PS like this:
ls -l (Get-Volume | ? FileSystemLabel -eq "Barry Allen drive").Path
It's shortened, use full format in a script for better readibility - see below
Using GptType
GPT does use fixed IDs for special partitions. We can use those for writing portable scripts, to access Recovery or System volumes on any computer without assigning it a letter:
System volume:ls -l (Get-Partition | ? GptType -eq "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}").AccessPaths[0]
Recovery volume:ls -l (Get-Partition | ? GptType -eq "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}").AccessPaths[0] -Force
cd "$((Get-Partition | ? GptType -eq "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}").AccessPaths[0])Recovery"
MSR: on UEFI, there is (should be) also MSR partition, but you can't access it as it does not have a volume nor a filesystem:
error: ls -l ((Get-Partition | ? GptType -eq "{e3c9e316-0b5c-4db8-817d-f92df00215ae}").AccessPaths[0]) -Force
Caveats
- Get-Volume will not list hidden volumes unless it's running elevated, Get-Partition will list them non-elevated, but you can't access them further unelevated anyway.
- With Get-ChildItem (ls) Device Path must be passed using -LiteralPath argument (-l). This is not because of '?' special character. -Path just does not work with Device Path even if '?' is escaped.
- It's not possible to CD into the root of Device Path. But it's possible to CD to its folder.
Explanation and full format of above statements
Always use full format in a script for better readibility
ls -l ((Get-Partition | ? GptType -eq "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}").AccessPaths[0])
is short for:
Get-ChildItem -LiteralPath ((Get-Partition | Where-Object { $_.GptType -eq "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}" }).AccessPaths[0])
ls
is an alias of Get-ChildItem
-l
is shortened -LiteralPath
? *attribute* -eq "*value*"
is shortened construct of ? { $_.*attribute* -eq "*value*" }
?
is an alias of Where-Object
-Force
is used to see hidden files on the Recovery volume
Use of Get-Partition and Get-Volume:
Get-Partition does provide GptType parameter, but not Label, Get-Volume gives Label, but not GptType