How to change CD ROM Letter?

FoxZ picture FoxZ · Oct 10, 2016 · Viewed 9.5k times · Source

I want to change letter from D to Z:

Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" ) -Arguments @{DriveLetter='Z:'}

Error:

Set-WmiInstance : Cannot bind argument to parameter 'InputObject' because it is
null.
At line:1 char:30
+ Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveL ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-WmiInstance], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetWmiInstance

Answer

Mathias R. Jessen picture Mathias R. Jessen · Oct 10, 2016

That error occurs because the WMI query doesn't return anything (probably because there's no device mounted at D:).

To avoid it, use the pipeline instead:

Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" |Set-WmiInstance -Arguments @{DriveLetter='Z:'}

If Get-WmiObject doesn't return anything then Set-WmiInstance won't run and you won't have any errors