Created
November 21, 2024 18:20
-
-
Save mmotti/eb68f7aa0e6028df932351bb9cd2c999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-IsAdminElevated { | |
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]:: | |
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
return $true | |
} else { | |
return $false | |
} | |
} | |
function Start-AdminElevatedTerminal { | |
Write-Warning "Attempting to relaunch the script with elevated privileges..." | |
$scriptPath = $script:MyInvocation.MyCommand.Path | |
$arguments = "new-tab -p `"PowerShell`" powershell -ExecutionPolicy Bypass -NoProfile -File `"$scriptPath`"" | |
if ($PSBoundParameters) { | |
$arguments += " $(@($script:MyInvocation.BoundParameters.GetEnumerator() | ForEach-Object { | |
if ($_.Value -is [switch]){ | |
"-$($_.Key)" | |
} elseif ($null -ne $_.Value) { | |
'-{0} `"{1}`"' -f $_.Key, $_.Value | |
} | |
}))" | |
} | |
Start-Process wt -ArgumentList $arguments -Verb RunAs | |
exit | |
} | |
if (!(Test-IsAdminElevated)) { | |
Start-AdminElevatedTerminal | |
} | |
# Get all physical disks and their models. | |
$physicalDisks = Get-WmiObject Win32_DiskDrive | Select-Object DeviceID, Model | |
# Get all volumes. | |
$volumes = Get-Volume | Where-Object { $null -ne $_.DriveLetter } | |
foreach ($volume in $volumes) { | |
# Get the associated partition | |
$partition = Get-WmiObject -Query "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='$($volume.DriveLetter):'} WHERE AssocClass=Win32_LogicalDiskToPartition" | |
if ($partition) { | |
# Get the physical disk associated with the partition | |
$disk = Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} WHERE AssocClass=Win32_DiskDriveToDiskPartition" | |
if ($disk) { | |
# Find the corresponding physical disk model | |
$model = $physicalDisks | Where-Object { $_.DeviceID -eq $disk.DeviceID } | Select-Object -ExpandProperty Model | |
if ($model) { | |
# Skip FAT formatted drives. 11 chars is often much too short. | |
if ($volume.FileSystem -in @("FAT32", "FAT16")) { continue } | |
# Max chars for most volumes including NTFS. | |
$maxChars = 32 | |
# Trim the model name if it's too long | |
$model = $model.Substring(0, [Math]::Min($maxChars, $model.Length)) | |
Write-Output "Drive Letter: $($volume.DriveLetter), Model: $model" | |
if ($volume.FileSystemLabel -ne $model) { | |
# Debug Output for Model and Drive Letter | |
Write-Output "Attempting to set volume label for Drive Letter: $($volume.DriveLetter), `"$model`"" | |
try { | |
# Set the volume label to the model name | |
Set-Volume -DriveLetter $volume.DriveLetter -NewFileSystemLabel $model -ErrorAction Stop | |
Write-Output "Success." | |
} catch { | |
Write-Error "Failed to set volume label for Drive Letter: $($volume.DriveLetter), Model: $model. Error: $_" | |
} | |
} else { | |
Write-Output "No action necessary." | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment