Get a partition name with PowerShell
Asked Answered
T

3

6

I have a flash drive which I formatted so that the volume label on the drive is "PHILIP".

enter image description here

I am using Get-PSDrive H -PSProvider FileSystem to determine if the drive is plugged in, however I would really like to determine if the drive is plugged in by the volume label, i.e. Get-PSDrive -VolumeLabel PHILIP -PSProvider FileSystem. Of course the VolumeLabel parameter does not exist so this doesn't work.

Is there a way to list the drives in a computer by volume name?

Truman answered 12/10, 2012 at 19:37 Comment(0)
B
8

You can use WMI, I guess:

Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }
Bespoke answered 12/10, 2012 at 19:42 Comment(0)
F
6

You can use the DriveInfo class from the .NET framework as well:

PS> [System.IO.DriveInfo]::GetDrives()
Name               : C:\
DriveType          : Fixed
DriveFormat        : NTFS
IsReady            : True
AvailableFreeSpace : 217269202944
TotalFreeSpace     : 217269202944
TotalSize          : 320070479872
RootDirectory      : C:\
VolumeLabel        : OS

You can then pipe that to the Where-Object cmdlet (both ? and Where are aliases) to filter that to just the volume you are looking for:

PS> [System.IO.DriveInfo]::GetDrives() | ? {$_.VolumeLabel -eq "PHILIP" }
Feline answered 12/10, 2012 at 20:29 Comment(0)
J
1

I use Get-WMIObject like Joey proposes. To link the wmi results to for example a get-partition i use the caption parameter. In this example I set the partition letter of volume Philip to D

$datavolume=Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }

$datavolume=$datavolume.Caption

get-partition -DiskNumber 0 | where {$_.accesspaths -like "$datavolume"} | Set-Partition -NewDriveLetter D

Jillene answered 20/12, 2019 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.