{"id":66573,"date":"2006-08-30T15:19:00","date_gmt":"2006-08-30T15:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/30\/how-can-i-use-windows-powershell-to-locate-all-the-wma-files-on-my-computer\/"},"modified":"2006-08-30T15:19:00","modified_gmt":"2006-08-30T15:19:00","slug":"how-can-i-use-windows-powershell-to-locate-all-the-wma-files-on-my-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-windows-powershell-to-locate-all-the-wma-files-on-my-computer\/","title":{"rendered":"How Can I Use Windows PowerShell to Locate All the .WMA Files on My Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" 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\"> \n<P>Hey, Scripting Guy! How can I use Windows PowerShell to locate all the .wma files on my computer?<BR><BR>&#8212; LC<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" 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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, LC. Harry Potter. Hogwarts. Ron Weasley. Dumbledore. Quiddich.<\/P>\n<P>What\u2019s all that about? Oh, nothing: just a few words that idly floated through the head of the Scripting Guy who writes this column. And before you ask, no, those words are <I>not<\/I> a less-than-subtle jab at the Scripting Editor, with whom he recently had a \u2026 difference of opinion \u2026 a difference of opinion revolving, in part, around these fictional constructs. As you might expect, the Scripting Guy who writes this column has nothing but the utmost respect for the Scripting Editor and for her opinions. <\/P>\n<P>Even if she <I>is<\/I> a bit of a <A href=\"http:\/\/en.wikipedia.org\/wiki\/Muggle\" target=\"_blank\"><B>Muggle<\/B><\/A>.<\/P>\n<P>So, no, there\u2019s no hidden agenda here. If anything, the Scripting Guy who writes this column mentioned Harry Potter simply because Harry and his friends are well acquainted with magic, and the Scripting Guy likes to think that the answer to your question is equally magical:<\/P><PRE class=\"codeSample\">Get-WMIObject Win32_LogicalDisk -filter &#8220;DriveType = 3&#8221; | \nSelect-Object DeviceID | \nForEach-Object {Get-Childitem ($_.DeviceID + &#8220;\\&#8221;) -include *.wma -recurse}\n<\/PRE>\n<TABLE id=\"END\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Of <I>course<\/I> she\u2019ll fall for that; why wouldn\u2019t she?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you\u2019ve no doubt heard, <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/msh.mspx\"><B>Windows PowerShell<\/B><\/A> is a new command shell\/scripting language released by the good folks here at Microsoft. (If you <I>haven\u2019t<\/I> heard about Windows PowerShell, you soon will; in fact, one of these days we\u2019ll have an important announcement right here in the Script Center.) Windows PowerShell is an interesting technology, and it has some definite strengths. Does that make it \u201cbetter\u201d than VBScript? That\u2019s like asking whether vanilla is better than chocolate: it depends on who you are and what you like. Either way, however, there\u2019s no doubt that Windows PowerShell can easily solve this particular problem, and with a minimal amount of effort.<\/P>\n<TABLE id=\"EGE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. For the record, chocolate is better than vanilla. Of course, the Scripting Editor will probably opt for flavorless gelatin as her favorite. But that\u2019s OK; she\u2019s fully-entitled to her opinion.<\/P>\n<P>Even if it <I>is<\/I> completely wrong.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s get down to business here. In order to locate all the .wma files on a computer we need to do three things:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Obtain a list of all the hard drives installed on the computer.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Grab the drive letter for each of these drives.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Search each drive for all the .wma files.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>With that in mind, why don\u2019t we start with step 1: obtaining a list of all the hard drives installed on the computer. (By the way, even though this looks like a couple different lines, the entire command should be typed on the same line.)<\/P>\n<P>Although there are a couple different ways we could retrieve the list of hard drives we decided to use WMI and the <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/msh\/cmdlets\/get-wmiobject.mspx\"><B>GetWMIObject<\/B><\/A> cmdlet; that\u2019s not only an approach that we knew would work, but also an approach that should be reassuringly-familiar to those of you who are proficient in VBScript but are new to Windows PowerShell. Here\u2019s the command that retrieves the list of disk drives found on the local computer:<\/P><PRE class=\"codeSample\">Get-WMIObject Win32_LogicalDisk -filter &#8220;DriveType = 3&#8221;\n<\/PRE>\n<P>As you can see, there\u2019s nothing too terribly difficult here. To begin with, we simply call the Get-WMIObject cmdlet, followed by the name of the WMI class we want to connect to (Win32_LogicalDisk). We then add the <B>\u2013filter<\/B> parameter, which is pretty much a WMI <I>Select * From<\/I> query minus the <I>Select * From<\/I> and the word <I>Where<\/I>. In VBScript we\u2019d use a query that looks like this:<\/P><PRE class=\"codeSample\">&#8220;Select * From Win32_LogicalDisk Where DriveType = 3\u201d\n<\/PRE>\n<P>In Windows PowerShell we use a filter that looks like this:<\/P><PRE class=\"codeSample\">&#8220;DriveType = 3&#8221;\n<\/PRE>\n<P>See? Nothing even <I>remotely<\/I> difficult about that.<\/P>\n<P>When we run this command we get back a collection consisting of all the properties of all the hard disks on the computer (any logical disk with a <B>DriveType<\/B> of 3 is a hard disk). That\u2019s great, except that all we really want is the drive letter for each of these hard disks. Therefore, we \u201cpipe\u201d this collection to the <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/msh\/cmdlets\/select-object.mspx\"><B>Select-Object<\/B><\/A> cmdlet (the | symbol represents the pipeline). What does that mean? That means we use Get-WMIObject to grab the collection, but instead of doing anything further using Get-WMIObject we hand the collection over to Select-Object. We then use <I>that<\/I> cmdlet to select <B>DeviceID<\/B>, the only property we care about:<\/P><PRE class=\"codeSample\">Select-Object DeviceID\n<\/PRE>\n<P>We still have a collection consisting of all the hard drives on the computer; it\u2019s just that we\u2019ve removed all the property values except for DeviceID. That leaves us with a collection similar to this:<\/P><PRE class=\"codeSample\">C:\nD:\nE:\n<\/PRE>\n<P>What we need to do now is pipe that collection once more, this time handing the trimmed-down collection to the <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/msh\/cmdlets\/foreach-object.mspx\"><B>ForEach-Object<\/B><\/A> cmdlet. The ForEach-Object cmdlet will then loop through the set of hard disks, using the <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/msh\/cmdlets\/get-childitem.mspx\"><B>Get-ChildItem<\/B><\/A> cmdlet against each drive in the collection. Get-ChildItem, as the name implies, is a cmdlet that can retrieve all the items found in a specified container, including all the files and subfolders in a folder.<\/P>\n<P>Here\u2019s what that command looks like:<\/P><PRE class=\"codeSample\">ForEach-Object {Get-Childitem ($_.DeviceID + &#8220;\\&#8221;) -include *.wma -recurse}\n<\/PRE>\n<P>In other words, for each drive we call Get-ChildItem, binding the cmdlet to the root folder (for example, C:\\). In order to specify the root folder we use <B>$_.DeviceID<\/B> followed by a \\. In case you\u2019re wondering, in Windows PowerShell $_ is a special variable that represents the object currently in use; the first time through the loop, that means that $_.DeviceID will be C:, the DeviceID property for item one in the collection. <\/P>\n<P>In addition we pass Get-ChildItem a pair of parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>-include *.wma<\/B>. This is a filter parameter that restricts returned data to files with a .wma file extension.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>-recurse<\/B>. This parameter causes Get-ChildItem to automatically search all the subfolders of the target folder; because the target folder is C:\\ that will result in the cmdlet searching all the folders on drive C.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<TABLE id=\"ETH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, in order to search all the folders on drive C: all you have to do is add the \u2013recurse parameter; there\u2019s no need to write a <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0218.mspx\"><B>recursive subroutine<\/B><\/A> like you do when using WMI or the FileSystemObject. Hmmm, maybe this <I>is<\/I> magic after all!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we run Get-ChildItem on drive C we then loop around and do the same thing with the next drive in the collection. Just exactly the same way a For Each loop works in VBScript. (The syntax is different, but the concept is the same.)<\/P>\n<P>By the way, you should find that this script runs pretty quickly. Give it a try and see for yourself.<\/P>\n<P>Now, where were we? Oh, that\u2019s right: Hagrid. Gryffindor. Voldemort. Hermione Granger.<\/P>\n<TABLE id=\"ELAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, we don\u2019t doubt that the Scripting Editor has a few words \u201crandomly\u201d floating through <I>her<\/I> head right about now. But as the Scripting Editor she has to censor things like that and can\u2019t say what she <I>really<\/I> wants to say, at least not here on TechNet. Take <I>that<\/I>, Scripting Editor!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<TABLE id=\"E6AAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Editor\u2019s Note<\/B>. Yes, the Scripting Editor does have plenty to say to the Scripting Guy that can\u2019t go on TechNet. But here are some things that can: Lawsuit; lawyers; copyright; trademark; unemployment; bankruptcy; living out of your car; living out of the Scripting Son\u2019s car. (We finally hit on a really scary one didn\u2019t we?)<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to locate all the .wma files on my computer?&#8212; LC Hey, LC. Harry Potter. Hogwarts. Ron Weasley. Dumbledore. Quiddich. What\u2019s all that about? Oh, nothing: just a few words that idly floated through the head of the Scripting Guy who writes this column. And before you [&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":[38,3,4,12,45,6],"class_list":["post-66573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to locate all the .wma files on my computer?&#8212; LC Hey, LC. Harry Potter. Hogwarts. Ron Weasley. Dumbledore. Quiddich. What\u2019s all that about? Oh, nothing: just a few words that idly floated through the head of the Scripting Guy who writes this column. And before you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66573","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=66573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66573\/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=66573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}