Get a partition name with PowerShell

14.3k views Asked by At

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?

3

There are 3 answers

0
Joey On BEST ANSWER

You can use WMI, I guess:

Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }
0
Goyuix On

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" }
0
Johan A. On

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