简体   繁体   中英

Get a partition name with PowerShell

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, ie 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?

你可以使用 WMI,我猜:

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

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" }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM