This is the 3th post of a series of 6 articles entitled “WMI Week“. It tends to covers the automation tasks around WMI mainly using the powershell WMI module.
The first gives a short introduction concerning WMI, and show what tools are usefull when working with WMI. The second post covers how to manage WMI namespaces using windows powershell. The third post (this one) highlights how we can, create, retrieve delete, and manage WMI classes. The fourth post covers everything you need to know about WMI properties. The fifth post will highlight everything related to WMI qualifiers, and the last and six’th post will explain everything you need the know about WMI Instances.
[important]By the way,the powershell WMI module is available here ( and Don’t forget to rate it by clicking on the stars ;)).[/important]
Rock steady ? let’s jump into it!
Haaa, Switzerland, what an amazing country. It has so much to offer. The people are extremly open minded, they have beautifull landscapes (See picture in WMI Week Day 2 for a for a concrete proof). They have those cool Swiss army knifes that help you to achieve all kind of tasks at home, in the woods, and perhaps even in space! (I actuallywrote a bitlocker encryption tool that works like a swiss army knife. check it out here ! –> Bitlocker swiss army knife script).
I talked about landscapes, I talked about swiss army knifes, but I didn’t mentionned the chocolate yet. Well let me tell you, it is A-MA-ZING !!
As a matter of fact, the company I work for has his main office just right above one of the best chocolate stores in town, and every morning, but I really mean EVERY MORNING,I pass in front of the store and this what I see before entering our building.
sometimes, it is reaaaaly difficult to resist the temptation.. 😉
Anyways, why are we here again for? ho yeah, WMI Classes. 3,2,1 GO!
Let’s continue our WMI week and talk about WMI Class.In today’s post, we will tackle the following topics:
- How to retrieve information from a custom WMI class?
- How to create / add a new WMI class?
- How to edit a WMI class?
- How to delete / remove a wmi class?
How to use powershell to find WMI classes?
What we will start by doing is to search for cmdlets that we can use in order to work with classes using the following command:
1
|
Get-WMICommands *class*
|
In this today’s post, we will focus on the following cmdlets:
- Get-WMIClass
- New-WMIClass
- Remove-WMIClass
Let’s explore the possiblities of the Get-WMIClass cmdlets.
1
|
Get-WMIClass
|
Using the Get-WMIClass cmdlet without any parameters will return you a huge list of classes. This list is actually the content of the “Rootcimv2” namespace, which is Powershell’s default WMI entry. It contained as showed in the below screen shot 1291 classes (on a Windows 8.1 machine).
[notice]It is important to know that this amount can vary from system to system depending on what has been installed on the machine. The count doesn’t differenciate system properties from the regular classes.[/notice]
If we want to have more information on let’s say the Win32_share class, we would write something like this:
1
|
Get-WMIClass -class win32_share
|
(Here, the use of -namespace is not necessary since Win32_share is located in “Rootcimv2” which happends to be default namespace of all of the cmdlets located in the WMI module 2.0.)
This will return all the properties, methods qualifiers that the class contains. BUT, it does NOT return the instances of the Win32_share.
[important]The properties that are returned here could be seen as something similar to a blueprint: Once we will instanciated it, we will have this list of properties that we will be able to use. The methodes here though can be used to execute different actions on the current instances that are already present. [/important] [notice]If you want to return the current present instance, in this case, the real shares currently present on the machine, you can use either Get-WMIClassInstance that is present in the WMI module, that will be covered in a later step, or the good old Get-WMIObject -class Win32_Share.[/notice]
How to create a new custom WMI class powershell?
Now that we have identified how we can get information about classes, let’s create our own one. You know which cmdlet we will need to use for that?
New-WMIClass is Correct! 😉
So, now that have create that first WMI NameSpace called “District“, let’s go ahead with this logic, and create our class directly in that namespace. Let’s name our custom WMI class with powershell to “PowerShellDistrict“. To achieve this, we will use the following small script:
1
|
New-WMIClass -NameSpace “RootDistrict” -Class “PowerShellDistrict”
|
The following will be returned:
Let’s check what we actually created using Get-WMIClass.
1
|
Get-WMIClass -Namespace RootDistrict -Class PowerShellDistrict
|
As the screen shot shows, it created the “PowerShelldistrict” class in the “RootDistrict” namespace. We can also see that the class has currently neither any methods, nor any Properties.
This class will the be used in our next posts on how to create WMI properties and qualifiers.
How to remove a WMI class with powershell?
Let’s say we want to delete our powershelldistrict wmi class with powershell, what cmdlet will we need to use? Correct! The Remove-WMIClass.
We wil use the following command to delete the class:
1
|
Remove-WMIClass -NameSpace Rootdistrict -ClassName PowerShellDistrict
|
The result would be the as the following:
In green, we see the built-in mecanism that asks for confirmation before deleting the property. In red the result.
[notice]For the purpose of my demonstration and the next sessions of the “WMI week“, I recreated the PowerShellDistrictClass so that we see how we can manage properties with PowerShell.[/notice]
Need more details ? check out the video that summerizes this page right here!
Ok, that is all for today. Let’s meet tomorrow again where I will go through how we can create, delete and modify WMI properties.[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
Leave A Comment