Set WmiInstance - PowerShell


Overview

Set-WmiInstance dynamically modifies the properties of an existing Windows Management Instrumentation (WMI) instance. It allows you to update property values in real time without requiring the creation or destruction of instances. This command is particularly useful for managing and configuring WMI-based objects and systems.

Syntax

Set-WmiInstance [-Namespace <string>] -Class <string> -Name <string> -Property <string[]> -Value <object[]> [-Credential <PSCredential>]

Options/Flags

  • Namespace: Specifies the WMI namespace where the instance resides. Defaults to root/cimv2.
  • Class: The name of the WMI class of the instance to be modified.
  • Name: The name or identifier of the instance to be modified. This corresponds to the value of the key property for the class.
  • Property: The name(s) of the property(ies) to be updated. Multiple properties can be specified as an array.
  • Value: The corresponding value(s) to set for the specified properties. Must correspond in number and order to the Property parameter.
  • Credential: A PSCredential object containing the credentials to use for accessing the WMI namespace.

Examples

Example 1: Updating a Single Property

Set-WmiInstance -Class Win32_ComputerSystem -Name "MyComputer" -Property Description -Value "Updated Computer Description"

Example 2: Updating Multiple Properties

$properties = "Description", "PartOfDomain"
$values = "New Computer Description", $false
Set-WmiInstance -Class Win32_ComputerSystem -Name "MyComputer" -Property $properties -Value $values

Example 3: Using Credentials

$cred = Get-Credential
Set-WmiInstance -Namespace "root/virtualization" -Class Msvm_VirtualSystem -Name "MyVM" -Property Memory -Value 8192 -Credential $cred

Common Issues

  • Ensure the provided Namespace and Class are correct. Incorrect values will result in an error.
  • Verify that the Property name(s) match existing properties of the specified class. Non-existent properties will cause an error.
  • Check that the Value data type matches the expected type for the specified property. Mismatched data types can lead to errors.
  • If using credentials, ensure they have sufficient privileges to modify the WMI instance.

Integration

Set-WmiInstance can be combined with other PowerShell commands to automate complex tasks.

  • Get-WmiObject: Retrieve WMI instances prior to modification.
  • Invoke-WmiMethod: Execute WMI methods on the modified instance.
  • Test-Wmi: Verify the modified instance’s properties and state.
  • New-WmiInstance: Creates a new WMI instance.
  • Get-WmiObject: Retrieves WMI instances.
  • Remove-WmiInstance: Removes WMI instances.
  • Invoke-WmiMethod: Executes WMI methods.