Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a partition name with PowerShell

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?

like image 904
Phil Avatar asked Oct 12 '12 19:10

Phil


People also ask

How do I get disk information in PowerShell?

To get the Windows disk information using PowerShell, we can use the WMI command or the CIM class command. You can see both the outputs are identical.

What is get PSDrive?

The Get-PSDrive cmdlet gets the drives in the current session. You can get a particular drive or all drives in the session. This cmdlet gets the following types of drives: Windows logical drives on the computer, including drives mapped to network shares.

What does Get Volume do?

The Get-Volume cmdlet will return a Volume object or a set of Volume objects that match the specified criteria. Note: Dynamic volumes are supported only by the following cmdlets: Repair-Volume (chkdsk), Optimize-Volume (defrag), and Format-Volume (format) on basic disks and storage spaces.


2 Answers

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" }
like image 27
Goyuix Avatar answered Nov 02 '22 23:11

Goyuix


You can use WMI, I guess:

Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }
like image 116
Joey Avatar answered Nov 03 '22 00:11

Joey