During the development of the script I posted yesterday, I noticed that a lot of people had problem discovering how to create WMI objects in PowerShell.

I found different suggestions, but they all had problems. When I have to create WMI object instances I use this this method, which I used in my old script about sharing folders

here is an example:

$SecDesc = ([WMIClass] "\\$ComputerName\root\cimv2:Win32_SecurityDescriptor").CreateInstance()

using configuration manager development we have to create a lot of wmi objects.

Take a look at this SDK Sample

in the VBScript example we have this line:

 ' Create and populate a temporary SMS_ServiceWindow object with the new maintenance window values. 
    Set tempServiceWindowObject = connection.Get("SMS_ServiceWindow").SpawnInstance_

 

and in the C# example it is this one

    // Create and populate a temporary SMS_ServiceWindow object with the new maintenance window values.
        IResultObject tempServiceWindowObject = connection.CreateEmbeddedObjectInstance("SMS_ServiceWindow");

in PowerShell the easy way to do it, is this:

#new service window
$serviceWindow = ([WMIClass] "\\$SCCM_SERVER\root\SMS\site_$($SCCM_SITECODE):SMS_ServiceWindow").CreateInstance()

I created this post to make it easier to find information about creating WMI object instances in PowerShell. Comments are welcome!