Drive Labels are the names attached to logical disks. Using WMI, you can both read and write (change) drive labels.
To read the existing drive label, use this function:
function Get-DriveLabel($letter='C:') {
if (!(Test-Path $letter)) {
Throw "Drive $letter does not exist."
}
if (!(Test-Path $letter)) {
Throw "Drive $letter does not exist."
}
([wmi]"Win32_LogicalDisk='$letter'").VolumeName
}
To actually change the drive label, try this one:
function Set-DriveLabel($letter='C:', $label='New Label') {
if (!(Test-Path $letter)) {
Throw "Drive $letter does not exist."
}
if (!(Test-Path $letter)) {
Throw "Drive $letter does not exist."
}
$instance = ([wmi]"Win32_LogicalDisk='$letter'")
$instance.VolumeName = $label
$instance.Put()
}
So to set a new drive label for disk D:, use this:
Set-DriveLabel D: 'A new label'
Be aware that changing a drive label requires Admin privileges. On Vista and above with UAC enabled, make sure you launch PowerShell with elevated rights.