Invoke WmiMethod - PowerShell


Overview

Invoke-WmiMethod invokes methods on Windows Management Instrumentation (WMI) classes to perform various operations on remote or local computers. It enables you to retrieve and modify data, configure settings, and control system components using WMI interfaces.

Syntax

Invoke-WmiMethod [-Class] <string> [-ComputerName] <string[]> [-Method] <string> [-Parameters] <psobject> [-Namespace] <string> [-Provider] <string> [-UseAmendedNamespace]

Options/Flags

-Class: Specifies the WMI class that contains the method to be invoked.

-ComputerName: (Optional) Specifies the name of the remote computer where the method will be invoked. Multiple computer names can be provided as an array.

-Method: Specifies the name of the method to be invoked.

-Parameters: (Optional) Specifies a hashtable of parameters to be passed to the method.

-Namespace: Specifies the WMI namespace where the class is located.

-Provider: Specifies the WMI provider to use when invoking the method. By default, the MSFT_WMIProvider provider is used.

-UseAmendedNamespace: (Optional) If specified, uses the amended namespace path for the WMI class instead of the canonical namespace path. This option should be used when working with classes that have been renamed or moved to a different namespace.

Examples

Get the BIOS version:

Invoke-WmiMethod -Class "Win32_BIOS" -Method "GetVersion"

Set the StartupType of a Windows service:

$params = @{"StartupType" = "Automatic"}
Invoke-WmiMethod -Class "Win32_Service" -Method "ChangeStartMode" -Parameters $params -ComputerName "RemoteComputer" -Namespace "root\cimv2"

Create a new user account:

$params = @{"Name" = "NewUserName"; "Password" = "NewUserPassword"}
Invoke-WmiMethod -Class "Win32_UserAccount" -Method "Create" -Parameters $params -ComputerName "LocalComputer"

Common Issues

WMI Permission Denied: Ensure that the user or account running PowerShell has sufficient permissions to perform the operation.

Invalid class or method: Verify the spelling and existence of the specified class and method.

WMI Provider Not Found: If the specified provider is not found, try using the -Provider parameter to specify the correct provider.

Integration

PowerShell DSC: Use Invoke-WmiMethod within PowerShell Desired State Configuration (DSC) scripts to configure WMI settings on target nodes.

WMI Command-Line Tools: Combine Invoke-WmiMethod with WMI command-line tools like wmic or wevtutil to automate complex tasks.

  • Get-WmiObject: Retrieves WMI objects.
  • Set-WmiInstance: Sets WMI instance properties.
  • New-WmiObject: Creates WMI instances.
  • WMI – Microsoft Docs