≡ Menu

Change Drive letter of CD Drive using Powershell

In my previous post, I talked about how to query CD ROM Drive letter using PowerShell. In this post, I will show you how to change the drive letter of CD Drive in remote computers. This will come handy when you want to assign a specific drive letter to CD ROM drive.

In the below code I am using two WMI classes (1) Win32_CDROMDrive (2) Win32_Volume. The first one I am using to get current drive letter of CD drive in remote computer. I am using that drive letter and passing it to Win32_Volume WMI class as filter to get a instance of CD Drive and then using Set-WMIInstance cmdlet to set the drive letter of CD Drive to new Drive letter that is given as parameter to below function (i.e Set-CDROMDriveLetter function).

This code works find for computers that has one CD drive connected. The logic needs a bit of extension if your remote computer is having multiple CD drives. I haven’t made my code to support multiple cd drives at this moment, but if you have a need to do so, post it in comments section and I will try to provide help when I get some time. Otherwise, the code works great for computers with single CD drive installed.

The function takes two arguments. (1) ComputerName (2) NewDriveLetter. The ComputerName is an optional parameter and can take a single computer name or list of computer names. It can also read the computername from pipe line. If this parameter is not specified, the script executes against local computer. The second parameter, NewDriveLetter , is the Drive letter that you want to assign to your CD ROM. It should have a column after the drive letter character like G: as shown the below example.

Code:

function Set-CDROMDriveLetter {            
[cmdletbinding()]            
param(            
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]            
    [string[]]$ComputerName = $env:COMPUTERNAME,            
    [parameter(Mandatory=$true)]            
    [string]$NewDriveLetter            
)            

begin {}            
process {            
    foreach($Computer in $COmputerName) {            
    $object = New-Object –TypeName PSObject –Prop(@{            
                'ComputerName'=$Computer.ToUpper();            
                'OldDriveLetter'= $null;            
                'NewDriveLetter'=$null;            
                'Status' = $null;            
               })            
    if(!(Test-Connection -ComputerName $Computer -Count 1 -ErrorAction 0)) {            
        Write-Verbose "$Computer is OFFLINE"            
        $object.Status = "OFFLINE"            
        $Object            
        Continue            
    }            
    try {            
        $cd = Get-WMIObject -Class Win32_CDROMDrive -ComputerName $Computer -ErrorAction Stop            
        $OldDriveLetter = $cd.drive            
        $cdvolume = Get-WmiObject -Class Win32_Volume -ComputerName $Computer -Filter "DriveLetter='$oldDriveLetter'" -ErrorAction Stop            
        Set-WmiInstance -InputObject $cdvolume -Arguments @{DriveLetter=$NewDriveLetter} -ErrorAction Stop | Out-Null            
        $Object.OldDriveLetter = $oldDriveLetter            
        $object.NewDriveLetter = $NewDriveLetter            
        $Object.Status = "Successful"            

    } catch {            
        Write-Verbose "Failed to Query WMI Class. $_"            
        $Object.Status = "Failed"            
        $Object            
        Continue;            
    }            

    $Object               

    }            
}            

end {}            

}

Output:

Hope this helps.. feedback welcome.

{ 0 comments… add one }

Leave a Comment