{"id":51823,"date":"2009-12-10T00:01:00","date_gmt":"2009-12-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/12\/10\/hey-scripting-guy-can-i-call-wmi-methods-more-easily-with-windows-powershell-2-0\/"},"modified":"2009-12-10T00:01:00","modified_gmt":"2009-12-10T00:01:00","slug":"hey-scripting-guy-can-i-call-wmi-methods-more-easily-with-windows-powershell-2-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-call-wmi-methods-more-easily-with-windows-powershell-2-0\/","title":{"rendered":"Hey, Scripting Guy! Can I Call WMI Methods More Easily with Windows PowerShell 2.0?"},"content":{"rendered":"<p class=\"MsoNormal\">&nbsp;<a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>     <\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I enjoy working with WMI, but it can be a bit complicated to make it do anything. It is great at retrieving information, and in conjunction with the Windows PowerShell <b>Get-WMIObject<\/b> cmdlet, it is really easy to obtain lots of information about our servers. In fact, WMI is one of the reasons we are deploying Windows PowerShell 2.0 to all of our 1,000 servers in our server farm. We feel that we will be able to get the ROI from the deployment project in a few months. I just wish it was easier to call the methods. <\/p>\n<p class=\"MsoNormal\">&#8212; JB<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello JB, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. There was <a href=\"http:\/\/en.wikipedia.org\/wiki\/Frost\"><font face=\"Segoe\">frost<\/font><\/a> on my front lawn this morning! Frost is about as close as we get to snow in sunny <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charlotte,_North_Carolina\"><font face=\"Segoe\">Charlotte, North Carolina<\/font><\/a>, in the United States. In honor of nearly having a &ldquo;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Weather-related_cancellation\"><font face=\"Segoe\">snow day<\/font><\/a>,&rdquo; I decided to make one of my favorite morning beverages. I brewed a small pot of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Espresso\"><font face=\"Segoe\">espresso<\/font><\/a> using coffee beans from <span lang=\"EN\"><a href=\"http:\/\/en.wikipedia.org\/wiki\/Chanchamayo_Province\"><font face=\"Segoe\">Chanchamayo<\/font><\/a> that I brought back from Peru. The coffee from high up in the Andes Mountains is especially rich and makes a fine espresso. I then heat milk in my double boiler, and as the temperature begins to rise, I take a bar of 85 percent cocoa chocolate and grate it into the milk. As I continue to stir the chocolate milk in the double boiler, I add two tablespoons of pure vanilla extract that I brought back with me from Cozumel, Mexico. While I am doing this, I fill a heavy stone coffee mug with water, and place it in the microwave and heat the water to warm my cup. I then pour the espresso into the heated stone coffee mug, and pour in my hot chocolate. I then grate a quarter of a stick of cinnamon and a little nutmeg to dust the top. It is a great way to start the morning. If you happen to have a slice of fresh baked <a href=\"http:\/\/en.wikipedia.org\/wiki\/Banana_bread\"><font face=\"Segoe\">banana nut bread<\/font><\/a> to go with it, it is even better. <\/span><\/p>\n<p class=\"MsoNormal\">JB, with a frosty crisp December morning, a cup of my special <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cafe_mocha\"><font face=\"Segoe\">mocha<\/font><\/a>, a slice of banana nut bread, and a question about WMI, what more could I ask for? It is going to be a festive day. I will play some <a href=\"http:\/\/en.wikipedia.org\/wiki\/Elvis_Presley\"><font face=\"Segoe\">Elvis<\/font><\/a> music on my Zune HD. Maybe I will even take the evening off and watch <a href=\"http:\/\/en.wikipedia.org\/wiki\/Blue_Hawaii\"><font face=\"Segoe\">Blue Hawaii<\/font><\/a> tonight. And&hellip;maybe not. <\/p>\n<p class=\"MsoNormal\">The great thing about using Windows PowerShell 2.0 to work with WMI methods is that it includes the <b>Invoke-WMIMethod<\/b> cmdlet. The <b>Invoke-WMIMethod<\/b> cmdlet makes it easier to call WMI methods, but it still requires an understanding of how WMI works. WMI has two types of methods: static methods, and instance methods. A static method is one that is always available, and an instance method is one that only works when an instance of the class is available&mdash;and it works on that instance. To illustrate the advantage of using the <b>Invoke-WMIMethod<\/b> cmdlet over the previous methods of working with WMI in Windows PowerShell 1.0, let us look at the old way and the new way of doing things. Keep in mind that both methodologies exist in Windows PowerShell 2.0, and it is perfectly legitimate to use the old style syntax. In certain cases, the old style syntax is actually easier to use and easier to understand. But we will get to that in a minute. <\/p>\n<p class=\"MsoNormal\">To create a new instance of a <b>Win32_Process<\/b> WMI class, you use the <b>create<\/b> method. The problem is that the <b>create<\/b> method is a static method. This fact is not mentioned on MSDN and is only discoverable by examining the qualifiers of the method in <b>WbemTest<\/b> as seen here:<\/p>\n<p class=\"Fig-Graphic\"><span><img decoding=\"async\" title=\"Image of qualifier in WbemTest\" alt=\"Image of qualifier in WbemTest\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1210\/hsg-12-10-09-01.jpg\" width=\"492\" height=\"283\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1210\/hsg-12-10-09-01.jpg\"><font face=\"Segoe\"><\/font><\/a><\/span><\/p>\n<p class=\"Fig-Graphic\"><br>It should not come as a surprise that the <i>create<\/i> method is static&mdash;if you think about it for a minute&mdash;to create a new process, you are not working with an individual process&mdash;you are working with the Win32_Process class. To delete a process however, you are working with an individual process&mdash;and the <i>terminate<\/i> method is an instance method. Understanding this difference helps to explain the two ways of working with WMI methods. Instance methods can be discovered by piping the results of the Get-WmiObject cmdlet to the Get-Member cmdlet. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_process | Get-Member -MemberType method<\/p>\n<p><br><span>&nbsp;&nbsp; <\/span>TypeName: System.Management.ManagementObject#rootcimv2Win32_Process<\/p>\n<p>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>MemberType Definition<br>&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br>AttachDebugger Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.ManagementBaseObject AttachDebugger()<br>GetOwner<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.ManagementBaseObject GetOwner()<br>GetOwnerSid<span>&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.ManagementBaseObject GetOwnerSid()<br>SetPriority<span>&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.ManagementBaseObject SetPriority(System.Int32 Priority)<br>Terminate<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.ManagementBaseObject Terminate(System.UInt32 Reason)<\/p>\n<p><br>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><br>The static methods are found by using the <b>[wmiclass]<\/b> type accelerator. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; [wmiclass]&#8221;Win32_Process&#8221; | Get-Member -MemberType method<\/p>\n<p><br><span>&nbsp;&nbsp; <\/span>TypeName: System.Management.ManagementClass#ROOTcimv2Win32_Process<\/p>\n<p>Name<span>&nbsp;&nbsp; <\/span>MemberType Definition<br>&#8212;-<span>&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br>Create Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>System.Management.ManagementBaseObject Create(System.String CommandLine, System.String CurrentDire&#8230;<\/p>\n<p><br>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><br>The difference between the <b>Get-WMIObject<\/b> cmdlet and the <b>[wmiclass]<\/b> type accelerator is that the <b>Get-WMIObject<\/b> cmdlet returns instances of the class, and the <b>[wmiclass]<\/b> type accelerator returns the class itself. <\/p>\n<p class=\"MsoNormal\">JB, let us put this together. To create a new process in Windows PowerShell 1.0, you use the the <b>[wmiclass]<\/b> type accelerator as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; ([wmiclass]&#8221;Win32_Process&#8221;).Create(&#8220;notepad&#8221;)<\/p>\n<p><br>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 2<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ProcessId<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 4924<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The syntax is rather terse, but is a bit confusing. In Windows PowerShell 2.0, if you want to create a new process, you can use the <b>Invoke-WMIMethod<\/b> cmdlet, which though a bit wordy is much easier to understand:<br><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Invoke-WmiMethod -Class win32_process -Name create -ArgumentList &#8220;notepad&#8221;<\/p>\n<p><br>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 2<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ProcessId<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 4592<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">In Windows PowerShell 1.0, if you want to terminate a process, you could use the <b>Win32_Process<\/b> WMI class and the <b>Get-WMIObject<\/b> cmdlet. Because <b>Terminate<\/b> is not a static method, it is available only on an instance of the class. To retrieve specific instances of the <b>Win32_Process<\/b> WMI class, you query for it by using the <b>Get-WmiObject <\/b>and a filter. The <b>&ndash;filter<\/b> parameter is used to limit the results to only processes named <b>notepad.exe<\/b>. Because multiple instances of the <b>Win32_Process<\/b> class were returned and an instance method only works on one instance, it is necessary to pipe the results to the <b>ForEach-Object<\/b> cmdlet. The <b>$_<\/b> automatic variable is used to refer to a single instance of the class at a time as it comes across the Windows PowerShell pipeline, and therefore the <b>Terminate<\/b> method can be called. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_Process -Filter &#8220;name = &#8216;notepad.exe'&#8221; | ForEach-Object { $_.Terminate() }<\/p>\n<p><br>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Using the <b>Invoke-WMIMethod<\/b> cmdlet to call instance methods is a bit more work. This is because the <b>&ndash;path<\/b> parameter must point to a specific instance of the class. Luckily, the path property is always available because it is a system property. In WMI all system properties are preceded by a double underscore, so they will filter to the top of a list of properties and can be easily discarded in search results. Note that the <b>__RELPATH<\/b> property refers to the WMI class name and a key property value on the local machine. The key property is always used to uniquely refer to instances of a WMI class. For the <b>Win32_Process<\/b> WMI class, the key property is <b>Handle<\/b><i>.<\/i> The <b>__PATH<\/b> property refers to a specific instance of the WMI class on a specific computer. The full path uses machine name, WMI namespace, WMI class, and the key. The system properties for one instance of the <b>Win32_Process<\/b> are shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_Process<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: CIM_Process<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: CIM_ManagedSystemElement<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_Process.Handle=&#8221;2208&#8243;<br>__PROPERTY_COUNT<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 45<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {CIM_Process, CIM_LogicalElement, CIM_ManagedSystemElement}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: MRED1<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: rootcimv2<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: \\MRED1rootcimv2:Win32_Process.Handle=&#8221;2208&#8243;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Because typing the path value can be a bit cumbersome, you can pick it up from the pipeline by using the <b>$_<\/b> automatic variable and choosing the <b>__Path<\/b> property. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject -Class win32_process -Filter &#8220;name = &#8216;notepad.exe'&#8221; | <br>ForEach-Object { Invoke-WmiMethod -Path $_.__Path &ndash;Name Terminate }<\/p>\n<p><br>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If you only have one instance of the WMI class you would like to delete, you can type the path to it directly. Because I am working on a local machine, I do not need to type the full <b>__PATH<\/b> value. I can use the <b>__RELPATH<\/b> value instead. The other thing that is significant is the <b>&ndash;Path<\/b> parameter of the <b>Invoke-WMIMethod<\/b> expects a WMI path for the value, and therefore quotation marks are not needed around the path. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Invoke-WmiMethod -Path win32_Process.Handle=&#8221;3944&#8243; -name terminate<\/p>\n<p><br>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">In fact, you do not need to set off the numbers of the handle with quotation marks either if you do not wish to do so. Leaving out all quotation marks is illustrated here. This is really helpful because one of the things that has always been confusing to scripters is the escaping of quotation marks inside other quotation marks. With the <b>Invoke-WMIMethod<\/b> cmdlet, these types of issues are avoided altogether: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Invoke-WmiMethod -Path win32_Process.Handle=3252 -name terminate<\/p>\n<p><br>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__SUPERCLASS <span>&nbsp;&nbsp;&nbsp;&nbsp;<\/span>:<br>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PROPERTY_COUNT : 1<br>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">JB, this should get you started using WMI methods. This also concludes WMI Week. Join us tomorrow when we dig into our virtual mailbag for Quick-Hits Friday. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I enjoy working with WMI, but it can be a bit complicated to make it do anything. It is great at retrieving information, and in conjunction with the Windows PowerShell Get-WMIObject cmdlet, it is really easy to obtain lots of information about our servers. In fact, WMI is one of [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,3,4,45,6],"class_list":["post-51823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I enjoy working with WMI, but it can be a bit complicated to make it do anything. It is great at retrieving information, and in conjunction with the Windows PowerShell Get-WMIObject cmdlet, it is really easy to obtain lots of information about our servers. In fact, WMI is one of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=51823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}