{"id":15321,"date":"2011-03-14T00:01:00","date_gmt":"2011-03-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/14\/change-drive-letters-and-labels-via-a-simple-powershell-command\/"},"modified":"2011-03-14T00:01:00","modified_gmt":"2011-03-14T00:01:00","slug":"change-drive-letters-and-labels-via-a-simple-powershell-command","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/change-drive-letters-and-labels-via-a-simple-powershell-command\/","title":{"rendered":"Change Drive Letters and Labels via a Simple PowerShell Command"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows you how to change drive letters and labels with a simple Windows PowerShell command.<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/p>\n<p>Hey, Scripting Guy! First of all, I want to thank you for being cool and for all the neat things you do with the Windows PowerShell community. Second of all, I was reading your <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/03\/11\/use-powershell-to-find-writable-wmi-properties.aspx\">blog last week<\/a> where you were talking about writable properties in <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394582(VS.85).aspx\">WMI<\/a>. You said it would allow you to do things. What kind of things did you have in mind? By the way, when can I sign up for the 2011 Scripting Games?<\/p>\n<p>&mdash;EB<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/>Hello EB, Microsoft Scripting Guy, Ed Wilson, here. Thank you for your kind words; I absolutely love working with the community. It lets me combine two of my favorite things&mdash;talking and Windows PowerShell. Therefore, I am happiest when I am talking about Windows PowerShell. <\/p>\n<p>EB, if you are as anxious about the 2011 Scripting Games as I am, you will want the following line of code&mdash;perhaps add it to your <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/profiles\/\">Windows PowerShell Profile<\/a> so that it runs every time you start Windows PowerShell. Here is the code and the associated results when it runs in the Windows PowerShell console.<\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">PS C:\\&gt; &#8220;$(([datetime]&#8217;4\/4\/11&#8242; -[datetime]::today).days) Days until 2011 Scripting Ga<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">mes&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">21 Days until 2011 Scripting Games<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>When I tweeted this, Microsoft MVP Tobias Weltner (@TobiasPSP) tweeted an easier way to obtain the previous information. Here is his code.<\/p>\n<blockquote>\n<p><span style=\"line-height: normal;list-style-type: disc;font-family:\"><span style=\"font-family: Lucida Sans Typewriter;color: #000000;font-size: 10pt\">(New-TimeSpan -End 4\/4\/11).days<\/span><\/span><\/p>\n<\/blockquote>\n<p>According to the text file of WMI methods and writable properties that I created in <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/03\/13\/get-all-methods-and-writable-properties-from-all-wmi-classes.aspx\">Sunday&rsquo;s Weekend Scripter<\/a> blog, there are three writable properties for the <b>Win32_Volume<\/b> WMI class. <\/p>\n<p>Keep in mind that the <b>Win32_Volume<\/b> WMI class does not appear in Windows XP. The <b>Win32_Volume<\/b> WMI class debuted with Windows Server 2003. The <b>Win32_LogicalDisk<\/b> WMI class provides some of the capabilities of the <b>Win32_Volume<\/b> WMI class, but not all of them. For example, by using <b>Win32_LogicalDisk<\/b>, you can change the <b>VolumeName<\/b> property, but not change the drive letter or enable indexing. <\/p>\n<p>The methods and properties for the class are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1323.HSG-3-14-11-01_39288E6C.jpg\"><img decoding=\"async\" height=\"564\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0741.HSG-3-14-11-01_thumb_42C0D6D5.jpg\" alt=\"Image of methods and properties\" border=\"0\" title=\"Image of methods and properties\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>There are several ways to use these properties. The way I have always used these types of properties in the past, is the way I did it in Windows PowerShell 1.0. That is, grab an instance of the object, store it in a variable, assign a new value to the property, and call the <i>put <\/i>method. The code to write a new label for drive E is shown here.<\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\"><span><span style=\"font-family: Lucida Sans Typewriter;color: #000000;font-size: 10pt\">$drive = gwmi win32_volume -Filter &#8220;DriveLetter = &#8216;E:'&#8221;<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\"><span><span style=\"font-family: Lucida Sans Typewriter;color: #000000;font-size: 10pt\">$drive.Label = &#8220;Bulk storage&#8221;<\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc;font-family:\"><span style=\"font-family: Lucida Sans Typewriter;color: #000000;font-size: 10pt\">$drive.put()<\/span><\/span><\/p><\/blockquote>\n<p>When I attempt to do this, an error message appears. The error, which is shown in the following image, is misleading. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5554.HSG-3-14-11-02_63F0CCB1.jpg\"><img decoding=\"async\" height=\"162\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8015.HSG-3-14-11-02_thumb_75ECEA71.jpg\" alt=\"Image of error message\" border=\"0\" title=\"Image of error message\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The reason for this error is that the Windows PowerShell prompt is not running with Administrator rights. Unfortunately, the error that bubbles back up from WMI does not tell us that the problem is related to rights. I want to change the label that is associated with drive E. The original label says <strong>New Volume<\/strong> as shown in the following image. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6064.HSG-3-14-11-03_65253FC3.jpg\"><img decoding=\"async\" height=\"386\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5305.HSG-3-14-11-03_thumb_36EB4409.jpg\" alt=\"Image of label\" border=\"0\" title=\"Image of label\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>I right-click my Windows PowerShell icon and click <b>Run As Administrator<\/b> from the Tasks list. When I open the Windows PowerShell console as an administrator and run the three commands, the drive E label updates. The command and its associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5305.HSG-3-14-11-04_1778ED76.jpg\"><img decoding=\"async\" height=\"244\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7065.HSG-3-14-11-04_thumb_69AB24B0.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The updated drive label is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7573.HSG-3-14-11-05_741BD303.jpg\"><img decoding=\"async\" height=\"386\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0640.HSG-3-14-11-05_thumb_41F482B9.jpg\" alt=\"Image of label\" border=\"0\" title=\"Image of label\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>However, by using the <b>Set-WmiInstance<\/b> Windows PowerShell cmdlet, I can update multiple properties at the same time by using a hash table. The easiest way to use <b>Set-WmiInstance<\/b> is to first use the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/03\/02\/how-can-i-use-wmi-with-windows-powershell.aspx\">Get-WmiObject<\/a> cmdlet to retrieve an input object that represents the object you wish to modify. In this case, the object is drive E, and it is an instance of the <b>Win32_Volume<\/b> WMI class. <\/p>\n<p>After I have the WMI object that represents the drive stored in the <i>$drive<\/i> variable, I call the <b>Set-WmiInstance<\/b> Windows PowerShell cmdlet and pass the <i>$drive<\/i> variable to the <i>input <\/i>parameter. The hash table consists of the properties that I want to modify and the new values for those properties. The code is shown here.<\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"line-height: 10pt;list-style-type: disc;margin: 4pt 0in 7pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$drive = Get-WmiObject -Class win32_volume -Filter &#8220;DriveLetter = &#8216;e:'&#8221;<\/span><\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc;font-family:\"><span style=\"font-family: Segoe\"><span style=\"color: #000000;font-size: 10pt\">Set-WmiInstance -input $drive -Arguments @{DriveLetter=&#8221;Q:&#8221;; Label=&#8221;Label&#8221;}<\/span><\/span><\/span><\/p><\/blockquote>\n<p>The command and its associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8585.HSG-3-14-11-06_384C1483.jpg\"><img decoding=\"async\" height=\"446\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7433.HSG-3-14-11-06_thumb_4E528015.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>One thing that is pretty cool is that when the drive letter has changed, my <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/windows\/windows-7\/default.aspx\">Windows 7<\/a> machine thinks a new drive has attached. Therefore, the <strong>AutoPlay<\/strong> dialog box appears, as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4380.HSG-3-14-11-07_18F947E3.jpg\"><img decoding=\"async\" height=\"818\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1172.HSG-3-14-11-07_thumb_1D231FA8.jpg\" alt=\"Image of dialog box\" border=\"0\" title=\"Image of dialog box\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The drive with its updated drive letter of <b>Q<\/b> and its updated drive label of <b>Label<\/b> are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0820.HSG-3-14-11-08_3897FBDE.jpg\"><img decoding=\"async\" height=\"386\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1680.HSG-3-14-11-08_thumb_27641E3B.jpg\" alt=\"Image of drive information\" border=\"0\" title=\"Image of drive information\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>As you can see, using the <b>Set-WmiInstance<\/b> Windows PowerShell cmdlet to modify writable WMI properties is easier and more intuitive than making multiple property value assignments (after you master the hash table). The cool thing is that multiple properties can be modified at one time instead of making multiple value assignments followed by the <i>put<\/i> method.<\/p>\n<p>EB, that is all there is to writing to WMI properties. Join me tomorrow when I will talk about calling WMI methods. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows you how to change drive letters and labels with a simple Windows PowerShell command. Hey, Scripting Guy! First of all, I want to thank you for being cool and for all the neat things you do with the Windows PowerShell community. Second of all, I was reading your [&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":[3,4,45,6],"class_list":["post-15321","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: Microsoft Scripting Guy, Ed Wilson, shows you how to change drive letters and labels with a simple Windows PowerShell command. Hey, Scripting Guy! First of all, I want to thank you for being cool and for all the neat things you do with the Windows PowerShell community. Second of all, I was reading your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15321","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=15321"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15321\/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=15321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}