PSTip: Change a drive letter using Win32_Volume class

Changing a drive letter of a removable drive or DVD drive is an easy task with Windows PowerShell. Using the Win32_Volume class and the Set-CimInstance cmdlet a drive letter can be reassigned. The following code will change the drive letter for the F: drive and change the drive letter to Z:

1
2
$DvdDrive = Get-CimInstance -Class Win32_Volume -Filter "driveletter='F:'"
Set-CimInstance -InputObject $DvdDrive -Arguments @{DriveLetter="Z:"}

Alternatively this can also be executed in a single line of code by using the pipeline:

1
2
Get-CimInstance -Query "SELECT * FROM Win32_Volume WHERE driveletter='F:'" | 
Set-CimInstance -Arguments @{DriveLetter="Z:"}

For more information about the Win32_Volume class have a look at the MSDN entry here:

Win32_Volume Class

Share on: