{"id":4460,"date":"2012-12-17T00:01:00","date_gmt":"2012-12-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/12\/17\/use-powershell-to-map-disk-drives-and-partitions\/"},"modified":"2012-12-17T00:01:00","modified_gmt":"2012-12-17T00:01:00","slug":"use-powershell-to-map-disk-drives-and-partitions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-map-disk-drives-and-partitions\/","title":{"rendered":"Use PowerShell to Map Disk Drives and Partitions"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and the CIM cmdlets to use WMI to map disk drives to partitions.\nMicrosoft Scripting Guy, Ed Wilson, is here. It is definitely a clich&eacute; that the more things change, the more they stay the same. This is certainly true in the world of computers. I have been an IT Pro for nearly a quarter of a century (dude, that sounds like a long time when I express it that way), and I have seen many changes in this fascinating field.\nBut the fact of the matter is that I am still doing the same sorts of things I did nearly forty years ago when I had an Osborne computer&mdash;I do databases, word processing, and spread sheets on a day-in and day-out basis. I also interact with other computers. Truth be told, even using the Scripting Wife&rsquo;s brand spanking new Surface, I am still doing word processing, spreadsheet databases, and interacting with other computers. And so it goes with scripting and with management tasks.\nOver the weekend, I was messing around with some of the computers on my network, and I realized that I did not have a mapping of partitions and disk drives &hellip; of course, the realization of that fact sprung from a troubleshooting need, but I digress.\nI thought there was an association WMI class that could do that, and so I used the CIM cmdlets to search the association classes, until I found what I needed.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;For more information on CIM classes, refer back to the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/12\/13\/use-powershell-cim-cmdlets-to-discover-wmi-associations.aspx\" target=\"_blank\">Use PowerShell CIM Cmdlets to Discover WMI Associations<\/a> blog post.\nThere is a VBScript version of what I am going to do in the Hey Scripting Guy blog post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2005\/05\/23\/how-can-i-correlate-logical-drives-and-physical-disks.aspx\" target=\"_blank\">How Can I Correlate Logical Disk Drives and Physical Disks<\/a><em>. <\/em>After you read that post, you will realize that my lament, that things stay the same, is for the types of tasks we confront and NOT for the tools we have to use. I mean, dude, that is some heavy duty code, and I have all the respect in the world for my Scripting Guys predecessors. See the way cool <a href=\"http:\/\/www.windowsitpro.com\/blog\/powershell-with-a-purpose-blog-36\/windows-powershell\/history-microsofts-scripting-guys-part-1-139746\" target=\"_blank\">History of the Scripting Guys<\/a> blog post for more information about the FAB FOUR (five)!<\/p>\n<h2>Mapping Disk Drives and Disk Partitions<\/h2>\n<p>The first thing to do when working with WMI association classes is to find out what the association is comprised of. To do this, use the <strong>Get-CimClass<\/strong> cmdlet, and pipe the results to the <strong>Select-Object<\/strong> cmdlet. Make sure you expand the <strong>CimClassProperties <\/strong>property. This command and its output associated are shown here.<\/p>\n<p style=\"padding-left: 30px\">16:49 C:&gt; Get-CimClass win32_diskdriveToDiskPartition | select -exp cimclassproperties<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Antecedent<\/p>\n<p style=\"padding-left: 30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">CimType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Reference<\/p>\n<p style=\"padding-left: 30px\">Flags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Property, Key, ReadOnly, NullValue<\/p>\n<p style=\"padding-left: 30px\">Qualifiers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {read, key, MappingStrings, Override}<\/p>\n<p style=\"padding-left: 30px\">ReferenceClassName : Win32_DiskDrive<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Dependent<\/p>\n<p style=\"padding-left: 30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">CimType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Reference<\/p>\n<p style=\"padding-left: 30px\">Flags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Property, Key, ReadOnly, NullValue<\/p>\n<p style=\"padding-left: 30px\">Qualifiers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {read, key, MappingStrings, Override}<\/p>\n<p style=\"padding-left: 30px\">ReferenceClassName : Win32_DiskPartition\nThe information I need is in the <strong>ReferencedClassName<\/strong> property. In fact, as I will show in just a minute, this information is also valuable from a troubleshooting perspective. I mean, I need to know what I am using in order to use it properly. Initially, I thought I could use <strong>Get-Disk<\/strong> and pipe it to the <strong>Get-CimAssociatedInstance<\/strong> cmdlet, but as is shown here, it does not work.<\/p>\n<p style=\"padding-left: 30px\">17:10 C:&gt; get-disk | Get-CimAssociatedInstance -Association win32_DiskDriveToDiskpartition\nNo error returns, but neither does any data. I then decided to look at <strong>Get-Disk<\/strong> to see what object it returns. I decide to look at the type object being returned. First, I use the <strong>GetType<\/strong> method as shown here.<\/p>\n<p style=\"padding-left: 30px\">17:13 C:&gt; (get-disk).GetType()<\/p>\n<p style=\"padding-left: 30px\">IsPublic IsSerial Name&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; BaseType<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&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; &#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; CimInstance&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; System.Object\nWell, that did no good. I now use <strong>Get-Member<\/strong>, because I know I have seen the underlying WMI classes before (and it is the actual returned WMI class that I suspect is causing problems). Here is the command I used and its associated output.\n17:13 C:&gt; (get-disk | get-member).typename[0]\nMicrosoft.Management.Infrastructure.CimInstance#ROOT\/Microsoft\/Windows\/Storage\/MSFT_Disk\nI see that the <strong>Get-Disk<\/strong> function returns a <strong>MSFT_Disk<\/strong> class and not an instance of the <strong>Win32_DiskDrive<\/strong> WMI class. So, I am back to basics. I use the <strong>Get-CimInstance<\/strong> cmdlet to return an instance of the <strong>Win32_DiskDrive<\/strong> WMI class. I pipe the results to the <strong>Get-CimAssociatedInstance<\/strong> cmdlet and specify the association as the <strong>win32_diskdriveToDiskPartition<\/strong> association class. The command and its associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">17:18 C:&gt; Get-CimInstance win32_diskdrive | Get-CimAssociatedInstance -Association w<\/p>\n<p style=\"padding-left: 30px\">in32_diskdriveToDiskPartition<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberOfBlock BootPartition PrimaryPartit Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Index<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s&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; ion<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;- &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Disk #0, Part&#8230; 716800&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 367001600&nbsp;&nbsp;&nbsp; 0<\/p>\n<p style=\"padding-left: 30px\">Disk #0, Part&#8230; 311859200&nbsp;&nbsp;&nbsp;&nbsp; False&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 159671910400 1\nThat is all there is to using Windows PowerShell, WMI, and the CIM classes to map disk drives and disk partitions.&nbsp; Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/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.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and the CIM cmdlets to use WMI to map disk drives to partitions. Microsoft Scripting Guy, Ed Wilson, is here. It is definitely a clich&eacute; that the more things change, the more they stay the same. This is certainly true in the world of [&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":[374,362,3,4,12,45,6],"class_list":["post-4460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disks","tag-powershell-3","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and the CIM cmdlets to use WMI to map disk drives to partitions. Microsoft Scripting Guy, Ed Wilson, is here. It is definitely a clich&eacute; that the more things change, the more they stay the same. This is certainly true in the world of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4460","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=4460"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4460\/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=4460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}