Change Drive Letter with PowerShell
-
Had an issue come up with a SAN tonight that mapped to the wrong drive letter and kept a server from coming up. Lots of fun. But it turned out that all I had to do was fixed the drive mapping. No GUI, though, so doing it from the command line was necessary. Here's how to do it.
First we can look at our disk devices with Get-Disk, but this just helps us understand what devices we have and if they are healthy. We can't see what drive letters or filesystems are associated with the devices from here. Think of this as being like lsblk on Linux.
> get-disk Number Friendly Name Serial Number HealthStatus OperationalStatus Total Size Partition Style ------ ------------- ------------- ------------ ----------------- ---------- ---------- 0 DELL PERC ... 008f58041e8dbf4d1900996254c0110b Healthy Online 465.25 GB GPT 1 DELL PERC ... 0006f12d0a4d56cc2000996254c0110b Healthy Online 3.64 TB GPT 2 WD My Book... WCC4EJLMCL37 Healthy Online 3.64 TB GPT
To see our file systems is gdr -PSProvider 'Filesystem', this is like the df command on Linux.
> gdr -PSProvider 'Filesystem' Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- C 98.47 366.22 FileSystem C:\ Users\admin D 3240.11 484.76 FileSystem D:\ F 2147.86 1577.99 FileSystem F:\ G FileSystem G:\
In my example above, we want to move the F:\ drive to be the E:\ drive. Here is how we do it.
$drv = Get-WmiObject win32_volume -filter 'DriveLetter = "F:"' $drv.DriveLetter = "E:" $drv.Put() | out-null
That's it. Not a one liner, but quite easy. PowerShell to the rescue. Run your gdr command again to see the change.
-
@scottalanmiller Why not just
get-psdrive
?PS C:\WINDOWS\system32> Get-PSDrive Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 55.11 24.35 FileSystem C:\ WINDOWS\system32 Cert Certificate \ D FileSystem D:\ E 971.83 928.05 FileSystem E:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE Variable Variable WSMan WSMan
-
A WD For backups?
-
-
What about using:
Get-Partition -DriveLetter F | Set-Partition -NewDriveLetter E
-