{"id":12341,"date":"2011-10-20T00:01:00","date_gmt":"2011-10-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/20\/use-the-set-wmiinstance-powershell-cmdlet-to-ease-configuration\/"},"modified":"2011-10-20T00:01:00","modified_gmt":"2011-10-20T00:01:00","slug":"use-the-set-wmiinstance-powershell-cmdlet-to-ease-configuration","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-set-wmiinstance-powershell-cmdlet-to-ease-configuration\/","title":{"rendered":"Use the Set-WmiInstance PowerShell Cmdlet to Ease Configuration"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to use the <strong>Set-WmiInstance<\/strong> Windows PowerShell cmdlet to ease configuration of computers.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><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\" \/><\/span>Hey, Scripting Guy! I was using the <b>Get-Command<\/b> cmdlet to look at what things I can do with Windows PowerShell, and I noticed a cmdlet called <b>Set-WmiInstance<\/b>. I looked at the help, but I don&rsquo;t get it. What is the big deal? <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&mdash;BL<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><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\" \/><\/span>Hello BL, <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I have had a lot of fun in Montreal this week. In a few days, we will be leaving and heading home. The Windows PowerShell class I have been teaching this week has been great, and the students have asked many good questions. In addition, they have given me many ideas for Hey, Scripting Guy! Posts for the future. These questions and ideas usually take the shape of, <i>I am trying to do such and such, but I am having a hard time finding out what I need to do.<\/i><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">BL, the <b>Set-WmiInstance<\/b> cmdlet is cool, but using it can be a bit complicated. The <i>class <\/i>parameter sets a property directly on a WMI class. Unfortunately, this works only on singleton WMI classes, of which there are very few. The one singleton class used by network administrators is <b>Win32_Operatingsystem<\/b>, but unfortunately, it does not work with the <i>class <\/i>parameter. Therefore, I only use the <b>Set-WmiInstance<\/b> cmdlet with the <i>path <\/i>parameter. The trick to using the <b>Set-WmiInstance<\/b> class with the <i>path <\/i>parameter is to know what a WMI path actually is and how to retrieve the WMI path for a WMI instance of a class. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><b>Note<\/b>&nbsp; &nbsp;In the help files, the example uses the <b>Win32_WMISetting<\/b>, and it changes the <b>logginglevel<\/b><i> <\/i>property. This works on Windows Server 2003 and earlier, but beginning with Windows Vista, WMI uses ETL logs and does not use this legacy parameter. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">One of the good things about working with a singleton class is that the path to the class is easy. The path is the WMI class name equals the @ symbol. This translates into syntax such as that shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">WMISingleTonClass=@<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">When using the <b>Set-WmiInstance<\/b> on a WMI class, the property to be set uses a hash table as it passes to the <i>arguments <\/i>parameter. The pairs are the property name and the value to assign to the property name. An example of this syntax is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">@{propertyName=Value}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">To assign a new description to the <b>Win32_OperatingSystem<\/b> singleton class, I perform the following steps (these steps require administrator rights on the target computer):<\/span><\/p>\n<ol>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I use the Set-WmiInstance class<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I supply the path to the Win32_OperatingSystem class to the <i>path <\/i>parameter<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I create a hash table using the WMI property name and the value to update the property with<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: Segoe;font-size: small\">The following command illustrates this technique:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Set-WmiInstance -Path win32_operatingsystem=@ -argument @{description=&#8221;iammred&#8221;}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">If I did not have the <b>Set-WmiInstance<\/b> cmdlet, I would need to use several steps to set the <b>Operating System<\/b> description property. The steps required to assign a new value to the <b>description<\/b><i> <\/i>property of the <b>Win32_OperatingSystem<\/b> class are shown here:<\/span><\/p>\n<ol>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Use the <b>Get-WmiObject<\/b> cmdlet to retrieve an instance of the <b>Win32_Operatingsystem<\/b> WMI class.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Store the returned management object in a variable.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Assign a new value for the <b>description<\/b><i> <\/i>property.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Call the <b>put<\/b><i> <\/i>method to write the information back to WMI.<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: Segoe;font-size: small\">The following commands illustrate this technique (the commands must run with elevated permissions):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$os = Get-WmiObject &ndash;class win32_operatingsystem<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$os.Description = &#8220;MyNewDescription&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$os.put()<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The situation changes a bit when working with a WMI class that is not a singleton. For example, if I query the <b>Win32_logicalDrive<\/b> WMI class on my laptop, three instances of the class appear. If I limit the results to only fixed local disks (<b>drivetype = 3<\/b>), I have two instances of the WMI class. The commands and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4784.hsg-10-20-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4784.hsg-10-20-11-1.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">To use the <i>path <\/i>parameter of the <b>Set-WmiInstance<\/b> cmdlet requires the path to a specific instance of the WMI class. The following WMI command returns the name of each logical disk and the path to each fixed logical disk. <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">PS C:\\&gt; Get-WmiObject -Class win32_logicaldisk -Filter {drivetype=3} | select name, __path<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"text-decoration: underline\">name<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__PATH<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">C:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \\\\MRED\\root\\cimv2:Win32_LogicalDisk.DeviceID=&#8221;C:&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Q:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\\\\MRED\\root\\cimv2:Win32_LogicalDisk.DeviceID=&#8221;Q:&#8221;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The important port to focus on right now is the result of the query that returns the <b>__path<\/b> property. The WMI path represented here comprises five parts. The parts in the <b>__Path<\/b> are shown in the following table.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"120\">\n<p><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Computer<\/span><\/b><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"128\">\n<p><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Namespace<\/span><\/b><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"128\">\n<p><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Class<\/span><\/b><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"128\">\n<p><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Key Property<\/span><\/b><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"115\">\n<p><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Value<\/span><\/b><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">\n<p><span style=\"font-family: Segoe;font-size: small\">\\\\MRED<\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"128\">\n<p><span style=\"font-family: Segoe;font-size: small\">root\\cimv2<\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"128\">\n<p><span style=\"font-family: Segoe;font-size: small\">Win32_LogicalDisk<\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"128\">\n<p><span style=\"font-family: Segoe;font-size: small\">DeviceID<\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"115\">\n<p><span style=\"font-family: Segoe;font-size: small\">C:<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">If I want to change the <b>Volumename<\/b> on the C: drive, I use the <b>Set-WmiInstance<\/b> cmdlet as shown here (this command must run with elevated rights):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Set-WmiInstance -Path &#8216;\\\\MRED\\root\\cimv2:Win32_LogicalDisk.DeviceID=&#8221;C:&#8221;&#8216; -argument @{VolumeName=&#8221;newlabel&#8221;}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6567.hsg-10-20-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6567.hsg-10-20-11-2.png\" \/><\/a><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Now, if I were to change the volume name using the <b>Get-WmiObject<\/b> cmdlet, I would use the syntax that is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$disk = Get-WmiObject -Class win32_logicaldisk -Filter &#8220;deviceID=&#8217;c:'&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$disk.VolumeName = &#8220;mycustomlabel&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$disk.Put()<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">In light of the long required path for the <b>Set-WmiInstance<\/b> command, it might appear the previous <b>Get-WmiObject<\/b> cmdlet exposes the property modification capability easier. It is possible to combine the best of both worlds. I use the <b>Get-WmiObject<\/b> cmdlet to retrieve the <b>__Path<\/b> property to the C: drive, and I store it in the <b>$path<\/b> variable. I then use that path with the <b>Set-WmiInstance<\/b> cmdlet. The resultant command is two lines instead of the three lines required for the other method. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command to combine the two techniques is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$path = (Get-WmiObject -Class win32_logicaldisk -Filter &#8220;deviceID=&#8217;c:'&#8221;).__path<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Set-WmiInstance -Path $path -argument @{VolumeName=&#8221;test&#8221;}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">In the following figure, the first line illustrates the technique of obtaining the <b>__path<\/b> directly from the <b>Get-WmiObject<\/b> cmdlet. The second line displays the path. The third line stores the path in the <b>$path<\/b> variable, and the fourth line is the <b>Set-WMiInstance<\/b> command. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8461.hsg-10-20-11-3.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands\" alt=\"Image of commands\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8461.hsg-10-20-11-3.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Well, BL, that is all there is to using the <b>Set-WmiInstance<\/b> cmdlet. I hope you found the discussion helpful. To everyone who has tuned in; I invite you to join me tomorrow.&nbsp;<\/span><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"color: #0000ff\">Twitter<\/span><\/a><\/span><span style=\"font-family: Segoe\"> and <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"color: #0000ff\">Facebook<\/span><\/a><\/span><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"color: #0000ff\">scripter@microsoft.com<\/span><\/a><\/span><span style=\"font-family: Segoe\">, or post your questions on the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"color: #0000ff\">Official Scripting Guys Forum<\/span><\/a><\/span><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-size: small\"><b><\/b><\/span>&nbsp;<\/p>\n<p><span style=\"font-size: small\"><b><\/b><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use the Set-WmiInstance Windows PowerShell cmdlet to ease configuration of computers. &nbsp; Hey, Scripting Guy! I was using the Get-Command cmdlet to look at what things I can do with Windows PowerShell, and I noticed a cmdlet called Set-WmiInstance. I looked at the help, but I don&rsquo;t get it. What is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,45,6],"class_list":["post-12341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use the Set-WmiInstance Windows PowerShell cmdlet to ease configuration of computers. &nbsp; Hey, Scripting Guy! I was using the Get-Command cmdlet to look at what things I can do with Windows PowerShell, and I noticed a cmdlet called Set-WmiInstance. I looked at the help, but I don&rsquo;t get it. What is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12341","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12341"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12341\/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=12341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}