3

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
FoxZ
  • 59
  • 1
  • 2
  • 6
  • Apparently `Get-WmiObject` doesn't return a result. You need to investigate why that is. – Ansgar Wiechers Oct 10 '16 at 18:28
  • @AnsgarWiechers can you please help in this. – FoxZ Oct 10 '16 at 18:29
  • On that system do you have a D: drive? What is the output of only `Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'"` – BenH Oct 10 '16 at 18:31
  • 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 Oct 10 '16 at 18:31

3 Answers3

4

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • This will check if exisint cd rom letter is D what is dynamically checking watever the exisitn letter may be change it to z: @mathia – FoxZ Oct 12 '16 at 10:58
2

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:'}
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

Try this for CD-ROM Update Powershell codes,

# Set CD/DVD Drive to A:
$cd = $NULL
$cd = Get-WMIObject -Class Win32_CDROMDrive -ComputerName $env:COMPUTERNAME -ErrorAction Stop
if ($cd.Drive -eq "D:")
{
   Write-Output "Changing CD Drive letter from D: to A:"
   Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" ) -Arguments @{DriveLetter='a:'}
}
BALA S
  • 31
  • 4