Remove CimInstance - PowerShell


Overview

Remove-CimInstance removes one or more instances of a specified Common Information Model (CIM) class from the local computer. It is primarily used to delete managed objects or configuration data represented as CIM instances.

Syntax

Remove-CimInstance [-CimClass] <string> [-Namespace] <string> [-Filter] <string> [-Property] <string[]> [-Force] [-Confirm] [-WhatIf] [-ThrottleLimit] <int>

Options/Flags

  • -CimClass: Specifies the CIM class of the instances to be removed.
  • -Namespace: Specifies the namespace from which to remove the instances. Defaults to “root\cimv2”.
  • -Filter: Specifies a Windows Management Instrumentation (WMI) filter query used to select the instances to be removed.
  • -Property: Specifies one or more property names to remove from the instances.
  • -Force: Suppresses the confirmation prompt and removes the instances without prompting for confirmation.
  • -Confirm: Prompts the user to confirm the removal of the instances.
  • -WhatIf: Shows what instances would be removed without actually removing them.
  • -ThrottleLimit: Sets the maximum number of concurrent instances that can be removed.

Examples

Remove a specific instance of the Win32_Service class:

Remove-CimInstance -CimClass Win32_Service -Filter "Name='SomeService'"

Remove all instances of the Win32_DiskDrive class from the root\cimv3 namespace:

Remove-CimInstance -CimClass Win32_DiskDrive -Namespace "root\cimv3"

Remove multiple instances of the Win32_Process class using a filter:

Remove-CimInstance -CimClass Win32_Process -Filter "ProcessID in (100, 200, 300)"

Remove specific properties from instances of the Win32_ComputerSystem class:

Remove-CimInstance -CimClass Win32_ComputerSystem -Property Caption,Description

Common Issues

  • Permission errors: Ensure that you have sufficient permissions to remove the instances.
  • Non-existent instances: Verify that the instances you are trying to remove exist.
  • Incorrect syntax: Double-check the syntax, especially when using filters or multiple options.

Integration

Remove-CimInstance can be integrated into scripts or command chains for automated management tasks. For example:

$removedInstances = Remove-CimInstance -CimClass Win32_Service -Filter "Status='Stopped'"
Get-CimInstance -CimClass Win32_Service -Filter "Status='Running'" | Remove-CimInstance -Confirm
  • Get-CimInstance: Retrieves CIM instances.
  • New-CimInstance: Creates new CIM instances.
  • Set-CimInstance: Modifies existing CIM instances.
  • CimCmdlets: Provides a suite of CIM-related cmdlets.