-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetDriveLetters.ps1
55 lines (49 loc) · 1.79 KB
/
SetDriveLetters.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Function to list all drive labels and their current letters
function Get-DriveLabels {
Write-Host "Current Drive Labels and Letters:"
Get-WmiObject -Query "SELECT Label, DriveLetter FROM Win32_Volume WHERE DriveLetter IS NOT NULL" | ForEach-Object {
$label = $_.Label
$letter = $_.DriveLetter
if ($label) {
Write-Host "Label: $label | Drive Letter: $letter"
} else {
Write-Host "Label: <No Label> | Drive Letter: $letter"
}
}
}
# Call the function to display drive labels and letters
Get-DriveLabels
# Define the list of drives with their target letters
$driveMap = @{
# "DriveLabel1" = "X"; # Example: Set drive labeled "DriveLabel1" to "X"
# Add more as needed
}
# Function to assign drive letter based on volume label
function Set-DriveLetter {
param (
[string]$Label,
[string]$TargetLetter
)
# Check if the target drive letter is already in use
if (Get-PSDrive -Name $TargetLetter -ErrorAction SilentlyContinue) {
Write-Host "Drive letter $TargetLetter is already in use."
return
}
# Find the drive by label and set its letter
$disk = Get-WmiObject -Query "SELECT * FROM Win32_Volume WHERE Label='$Label'" -ErrorAction SilentlyContinue
if ($disk) {
try {
$disk.DriveLetter = $TargetLetter + ":"
$disk.Put() | Out-Null
Write-Host "Drive '$Label' assigned to $TargetLetter:"
} catch {
Write-Host "Failed to assign $TargetLetter to drive '$Label': $_"
}
} else {
Write-Host "Drive with label '$Label' not found."
}
}
# Iterate through each drive label and assign the target letter
foreach ($label in $driveMap.Keys) {
Set-DriveLetter -Label $label -TargetLetter $driveMap[$label]
}