I have a flash drive which I formatted so that the volume label on the drive is "PHILIP".
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?
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.
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.
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.
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" }
You can use WMI, I guess:
Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With