1

Every time I write a script, or code that uses WMI extensively, I tend to use the System.Management API. It's straight forward, flexible, and intuitive.
However, This time I decided to use the Microsoft.Management.Infrastructure API, which contains the famous CimCmdlets.
Part of my script needs to delete a CIM Class, but I can't find how, using only CIM.
I know there's the System.Management.ManagementClass.Delete() method, but can't seem to find something similar.
I tried Microsoft.Management.Infrastructure.CimSession.DeleteInstance(), but of course it doesn't work.
Can't find documentation on it anywhere. Any ideas?

Edit: I want to delete the class itself, not the class instancess.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
FranciscoNabas
  • 505
  • 3
  • 9

1 Answers1

0

Note:

  • This answer does not address the question at hand, namely how to delete a WMI / CIM class, specifically via APIs in the Microsoft.Management.Infrastructure namespace rather than via System.Management (see the comments for how to do it via the latter).

  • It instead addresses how to delete an instance of such a class - which may hopefully be of interest to some future readers nonetheless.


A major - somewhat unfortunate - difference between the legacy WMI cmdlets and their modern CIM successors is that the objects returned by the latter do not have methods - and therefore also no .Delete() method.

The CIM cmdlets therefore require dedicated cmdlets for invoking methods and removing instances, namely Invoke-CimMethod and Remove-CimInstance.

For more information, see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    It throws saying the '$yourClassName' does not contain a method called 'Delete'. It's looking for the method in the class definition. – FranciscoNabas May 30 '23 at 20:26
  • @FranciscoNabas, good to know. Come to think of it: since CIM is just a layer on top of WMI on Windows, you should still be able to use something like `[System.Management.ManagementClass]::new('CIM_SomeClass').Delete()` - or is there a reason you want to avoid that? Or does that not work either? – mklement0 May 30 '23 at 20:35
  • 1
    Yes, that does the trick, you can also use it's type accelerator: `([wmiclass]'Root\Namespace:Cim_SomeClass').Delete()`, however I wan't to know if it's possible using the other API, for consistency sake. The MI namespace is built on top of a lower level "provider" built specifically for this. You can also use it in unmanaged code, that's why is faster. `System.Management` was built on the existing WMI APIs, if I'm not mistaken. – FranciscoNabas May 30 '23 at 20:40