{"id":8561,"date":"2012-07-30T00:01:00","date_gmt":"2012-07-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/07\/30\/use-powershell-to-explore-nested-directories-and-files\/"},"modified":"2012-07-30T00:01:00","modified_gmt":"2012-07-30T00:01:00","slug":"use-powershell-to-explore-nested-directories-and-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-explore-nested-directories-and-files\/","title":{"rendered":"Use PowerShell to Explore Nested Directories and Files"},"content":{"rendered":"<p><b>Summary<\/b>: The Microsoft Scripting Guy talks about using the Get-ChildItem cmdlet to find, sort, and count folders.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. In the summer, one&rsquo;s heart turns to, well, numbers if one happens to work for Microsoft. Yes, it is review season. During review season, one has to figure out everything he (or she) has done during the previous year. So if you are a Scripting Guy, part of that has to do with how many Hey, Scripting Guy! Blog posts you wrote, how many Windows PowerShell scripts you wrote, how many screenshots you took, and so on. Luckily, they do not ask how many Tim-Tams I ate or how many cups of tea I drank. (I could probably figure it out, but I would not be able to use Windows PowerShell to do that. We have not yet written a kitchen provider). Today I am going to answer the question of how long have I been writing the Hey, Scripting Guy! Blogs.<\/p>\n<h2>Three secrets to using the Get-ChildItem cmdlet<\/h2>\n<p>When you use the <b>Get-ChildItem<\/b> cmdlet, there are three things you must know that are not immediately obvious:<\/p>\n<ol>\n<li>Use the <b>force<\/b><i> <\/i>parameter to view hidden or system files.<\/li>\n<li>Use the <b>recurse<\/b><i> <\/i>parameter to see subdirectories and nested files.<\/li>\n<li>Use the <b>psIsContainer<\/b><i> <\/i>parameter to see only directories.<\/li>\n<\/ol>\n<p>In the output shown here, the <b>dir<\/b><i> <\/i>command (an alias for the <b>Get-ChildItem<\/b> cmdlet) returns only a few items.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Mode &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">d-r&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/22\/2012&nbsp;&nbsp; 7:18 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/21\/2012&nbsp; 11:41 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; files<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/23\/2012&nbsp;&nbsp; 5:52 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fso<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;7\/22\/2012&nbsp;&nbsp; 8:55 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Intel<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/14\/2012&nbsp;&nbsp; 9:48 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PerfLogs<\/p>\n<p style=\"padding-left: 30px\">d-r&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/22\/2012&nbsp;&nbsp; 8:55 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Program Files<\/p>\n<p style=\"padding-left: 30px\">d-r&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/22\/2012&nbsp;&nbsp; 8:55 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Program Files (x86)<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/22\/2012&nbsp;&nbsp; 5:33 PM&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans<\/p>\n<p style=\"padding-left: 30px\">d-r&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/22\/2012&nbsp;&nbsp; 8:56 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Users<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/23\/2012&nbsp; 11:26 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VMs<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7\/22\/2012&nbsp;&nbsp; 8:56 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Windows<\/p>\n<h2>Use the Force to see hidden and system files<\/h2>\n<p>On the other hand, when I use the <b><span style=\"text-decoration: underline\">force<\/span><\/b><i> <\/i>parameter, many more items return. This action corresponds with the <b>Show hidden and system files<\/b><i> <\/i>options in the folder view tool for Explorer. The thing is, this is an easy switch to forget, and one that really makes you feel foolish when someone reminds you of that oversight. It is a <a href=\"https:\/\/www.youtube.com\/watch?v=khSIYmTzt6U\" target=\"_blank\">Homer Simpson doh! moment<\/a>. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Dir -force<\/p>\n<p>The command the output associated with the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8666.HSG-7-30-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8666.HSG-7-30-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Burrow into nested folders<\/h2>\n<p>If I need to burrow down into a directory structure, I need to use the <b>recurse<\/b><i> <\/i>parameter. If I go back to my previous command from the root of the C:\\ drive, and I add the <b>recurse<\/b><i> <\/i>switch, it will return every single file on the entire computer hard disk drive, and will therefore take a bit of time. If, for example, I change to the ScriptingGuys folder, and I use the <b>recurse<\/b><i> <\/i>parameter, I might use a command such as the one shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path C:\\data\\ScriptingGuys -recurse<\/p>\n<p>The output from the command begins by listing all of the folders under the ScriptingGuys folder. Then it lists files that appear in the root of the ScriptingGuys folder. When that process completes, it begins to burrow into the other folders. The command and the initial output from the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8686.HSG-7-30-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8686.HSG-7-30-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Finding only folders or directories<\/h2>\n<p>To only return folders (or directories&hellip;or whatever we call them), use the <b>psIsContainer<\/b><i> <\/i>property. This property returns a Boolean value, and it is therefore easy to toss into a <b>Where-Object <\/b>filter. The command to return only folders within my ScriptingGuys directory is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path C:\\data\\ScriptingGuys -recurse | where {($_.psiscontainer)}<\/p>\n<p>In Windows PowerShell 3.0, the command is simplier due to leaving off the braces and the <strong>$_<\/strong> character. The syntax is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path C:\\data\\ScriptingGuys -recurse | where psiscontainer<\/p>\n<p>The command to return nested folders and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5430.HSG-7-30-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5430.HSG-7-30-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I create a folder each week for my Hey, Scripting Guy! blogs. Suppose I want to know how long I have been writing the Hey, Scripting Guy! Blogs. One way to get this information is to figure out how many folders I have. Because I use a Windows PowerShell script to create and name my folders, I am assured that they are all named the same. (I wrote the script after I became the Scripting Guy, so a few of the first folders are not all capitalized the same.) To do this, I use the <b>Get-ChildItem<\/b> cmdlet to find the folders, and I pipe the resulting <b>DirectoryInfo<\/b> objects to the <b>Measure-Object<\/b> cmdlet. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-ChildItem -Path C:\\data\\ScriptingGuys -recurse | where {($_.psiscontainer)}&nbsp; |<\/p>\n<p style=\"padding-left: 30px\">Measure-Object<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 289<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Property :<\/p>\n<p>But as we saw, there are folders that do not begin with HSG in the 289 folder count. I created these extra folders for things like the Scripting Games. So to remove them from the count, I use a simple Regular Expression pattern (<b>&lsquo;^HSG&rsquo;<\/b>). The carrot here means that the folder name does not begin with the letters HSG&mdash;so that would be all of my Scripting Games folders and associated folders for articles that are not related to the Hey, Scripting Guy! Blog.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>This points to the value of using a Windows PowerShell script to do routine admin tasks. You can be certain that they are all accomplished in the same manner, and it gives you great value when you later need to use a script to gather that information.<\/p>\n<p>The resulting command to find all of the folders that are not HSG folders is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-ChildItem -Path C:\\data\\ScriptingGuys -recurse | where {$_.psiscontainer<\/p>\n<p style=\"padding-left: 30px\">-AND $_.name -notmatch &#8216;^hsg&#8217;} | measure<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 75<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Property :<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;Because I did not specify a case sensitive search, the pattern <b>^hsg<\/b> works the same as <b>^HSG<\/b>.<\/p>\n<p>I now subtract the original number of folders from the non-HSG folders, and arrive at the answer as to how long I have been writing the Hey, Scripting Guy! Blog. The results are in the image shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2553.HSG-7-30-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2553.HSG-7-30-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Playing with Files Week will continue tomorrow when I explore some more cool stuff.<\/p>\n<p>I 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=\"mailto: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.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The Microsoft Scripting Guy talks about using the Get-ChildItem cmdlet to find, sort, and count folders. Microsoft Scripting Guy, Ed Wilson, is here. In the summer, one&rsquo;s heart turns to, well, numbers if one happens to work for Microsoft. Yes, it is review season. During review season, one has to figure out everything he [&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":[38,3,4,45],"class_list":["post-8561","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: The Microsoft Scripting Guy talks about using the Get-ChildItem cmdlet to find, sort, and count folders. Microsoft Scripting Guy, Ed Wilson, is here. In the summer, one&rsquo;s heart turns to, well, numbers if one happens to work for Microsoft. Yes, it is review season. During review season, one has to figure out everything he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8561","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=8561"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8561\/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=8561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}