#PSTip Another way to modify WMI instance properties

In the previous tip we showed you how to modify WMI object properties using Get-WmiObject cmdlet and the Put method. Today I want to show you another streamlined way to do the same using the Set-WmiInstance cmdlet.

The Set-WmiInstance cmdlet creates or updates an instance of an existing WMI class and writes the updates back to the WMI repository. So, if we wanted to modify the volume name of drive D:

Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='D:'" |
Set-WmiInstance -Arguments @{VolumeName = "Data"}

The Arguments parameter accepts a hashtable of name-value pair, property names, and the new values we want to set. To update multiple properties, delimit them with a semicolon @{Property1=’Value1′; Property=’Value2′}

Note: You must have administrative rights to update the properties and the console must be run elevated.

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts