2
votes

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
2
Apparently Get-WmiObject doesn't return a result. You need to investigate why that is. - Ansgar Wiechers
@AnsgarWiechers can you please help in this. - FoxZ
On that system do you have a D: drive? What is the output of only Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" - BenH
How would you expect me to do that? I'm not sitting in front of your computer. What output does Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" produce if you run it by itself? What output does Get-WmiObject -Class Win32_Volume | select DriveLetter produce? - Ansgar Wiechers

2 Answers

4
votes

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

1
votes

Apparently the nested Get-WmiObject call doesn't return a result. There could be a number of reasons for this:

  • system doesn't have an optical drive
  • system has an optical drive, but with a different drive letter
  • an error occurred (but you set the error action to SilentlyContinue)

Generally a better approach is to not rely on the drive letter, but the drive type, and -as Mathias pointed out in his answer- use a pipeline instead of nesting the command (so that empty results are skipped over instead of throwing an error).

Get-WmiObject -Class Win32_volume -Filter 'DriveType=5' |
  Select-Object -First 1 |
  Set-WmiInstance -Arguments @{DriveLetter='Z:'}