Remove WmiObject - PowerShell


Overview

Remove-WmiObject is a PowerShell command that removes specified WMI objects from the WMI repository. It enables efficient management of WMI objects by deleting obsolete, duplicate, or unwanted entries.

Syntax

Remove-WmiObject [-Namespace <string>] -Class <string> [-Filter <string>]

Options/Flags

| Option | Description | Default |
|—|—|—|
| -Namespace | Specifies the WMI namespace from which to remove objects. | ROOT/CIMV2 |
| -Class | Specifies the WMI class of objects to remove. | Required |
| -Filter | Applies a WQL filter to select specific objects within the class to remove. | Optional |

Examples

Example 1: Remove all objects of a specified class

Remove-WmiObject -Class "Win32_OperatingSystem"

Example 2: Remove objects based on a filter

Remove-WmiObject -Class "Win32_Process" -Filter "Name = 'notepad.exe'"

Example 3: Remove objects from a specific namespace

Remove-WmiObject -Namespace "root/cimv3" -Class "Win32_PageFileUsage"

Common Issues

Error: “Not found”

This error occurs when the specified class or object does not exist in the WMI repository. Ensure that the class name and filter (if used) are correct.

Error: “Access denied”

You may encounter this error if you lack sufficient permissions to remove the WMI objects. Verify that your user account has the necessary privileges.

Integration

Combine with “Get-WmiObject” to validate removal

$objects = Get-WmiObject -Class "Win32_Process" -Filter "Name = 'notepad.exe'"
Remove-WmiObject -Class "Win32_Process" -Filter "Name = 'notepad.exe'"
Get-WmiObject -Class "Win32_Process" -Filter "Name = 'notepad.exe'" -ErrorAction SilentlyContinue
  • Get-WmiObject: Retrieves WMI objects.
  • New-WmiObject: Creates new WMI objects.
  • Set-WmiInstance: Modifies existing WMI objects.