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:

$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:

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

About the author: Jaap Brasser

Jaap is a Senior System Engineer in the Financial Services industry. He focuses on Microsoft and related technologies and has a passion for PowerShell. In his spare time he maintains his Blog, Twitter feed, @Jaap_Brasser, and contributes on the Official Scripting Guys Forum and Windows PowerShell TechNet forums

Related Posts