{"id":13315,"date":"2017-01-30T06:00:22","date_gmt":"2017-01-30T14:00:22","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=13315"},"modified":"2017-01-28T13:25:10","modified_gmt":"2017-01-28T21:25:10","slug":"friday-puzzle-why-doesnt-include-include","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2017\/01\/30\/friday-puzzle-why-doesnt-include-include\/","title":{"rendered":"Friday Puzzle: Why doesn\u2019t Include include?"},"content":{"rendered":"<p>On Friday, January 20, 2017, I posted the following puzzle on <a href=\"https:\/\/twitter.com\/juneb_get_help\/status\/822448851579596800\" target=\"_blank\">Twitter <\/a>and <a href=\"https:\/\/www.facebook.com\/SAPIENTech\/photos\/a.10151095012027283.433079.82797372282\/10154242716222283\/?type=3\" target=\"_blank\">Facebook <\/a>(and <a href=\"https:\/\/www.facebook.com\/groups\/powershell\/permalink\/1321915161199992\/?match=cHV6emxlLGZyaWRheQ%3D%3D\" target=\"_blank\">here<\/a>!).<\/p>\n<p>It shows a <a href=\"http:\/\/technet.microsoft.com\/library\/hh847897(v=wps.630).aspx\" target=\"_blank\">Get-ChildItem<\/a> command where the value of the <strong>Path<\/strong> parameter is $PSHome, the Windows PowerShell installation directory, and the value of the <strong>Include<\/strong> parameter is &#8216;*ps1xml.&#8217; The command returns nothing &#8212; not a single file &#8212; even though the next command shows that there are 14 *.ps1xml files in the root of the $PSHome directory.<\/p>\n<p>What happened here? Why didn&#8217;t the Get-ChildItem -Include command work?<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-13316\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image001.png\" alt=\"\" width=\"468\" height=\"245\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image001.png 800w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image001-300x157.png 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image001-768x402.png 768w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p>This might enlighten you. Or, confuse you even more. In this command, we add &#8216;\\*&#8217; to the path, which indicates its immediate subfolders. When you add &#8216;\\*&#8217;, Get-ChildItem gets the .ps1xml files in its <strong><em><u>root<\/u><\/em><\/strong> folder.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image003.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-13317\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image003.png\" alt=\"\" width=\"460\" height=\"216\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image003.png 800w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image003-300x141.png 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image003-768x360.png 768w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<p>And, adding <strong>-Recurse<\/strong> works, too, although it gets all .ps1xml files in all subdirectories of $PSHome recursively. And, sadly the <strong>Depth<\/strong> parameter (new in PowerShell 5.0) seems to have no effect on Get-ChildItem when -Include or -Exclude are used (not documented).<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-ChildItem -Path $PSHome -Include '*ps1xml' -Recurse<\/pre>\n<p>What&#8217;s going on here?<\/p>\n<p>&nbsp;<\/p>\n<h1>About the Include Parameter<\/h1>\n<p>To solve the mystery, look at help for the <strong>Include<\/strong> parameter. I worked really hard to get this parameter description right, so I remember it well.<\/p>\n<p>As we&#8217;ve found, the <strong>Include<\/strong> parameter works only when the path includes &#8220;\\*&#8221; or the command includes the <strong>Recurse<\/strong> parameter.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image005.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-13318\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image005.png\" alt=\"\" width=\"487\" height=\"191\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image005.png 790w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image005-300x117.png 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image005-768x300.png 768w\" sizes=\"auto, (max-width: 487px) 100vw, 487px\" \/><\/a><\/p>\n<p>The coolest thing about the <strong>Include<\/strong> and <strong>Exclude<\/strong> parameters are that they take an array of values, and include or exclude all items in the array.<\/p>\n<pre class=\"output\">PS C:\\ &gt; Get-ChildItem -Path $PSHOME\\* -Include \"*.xml\", \"*.txt\"<\/pre>\n<p><strong>TIP: <\/strong>Exclude does not require -Recurse or &#8216;\\*&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Include<\/strong> is critical in PowerShell 2.0, because the <strong>Recurse<\/strong> parameter doesn&#8217;t work on items that have no child-items.<\/p>\n<pre class=\"output\"><span style=\"color: lightgreen;\"># This fails<\/span>\r\nPS C:\\&gt; Get-ChildItem -Path $PSHome\\*.ps1xml -Recurse<\/pre>\n<p>In Windows PowerShell 2.0, you need to do this:<\/p>\n<pre class=\"output\"><span style=\"color: lightgreen;\"># This works<\/span>\r\nPS C:\\&gt; Get-ChildItem -Path $PSHome\\* -Recurse -Include '*ps1xml'<\/pre>\n<h1>Which parameters can you use?<\/h1>\n<p>To get .psm1xml files in a directory, you can specify the file name extension on the Path parameter.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-ChildItem -Path $PSHome\\*.ps1xml<\/pre>\n<p>&nbsp;<\/p>\n<p>Beginning in PowerShell 3.0, you can use the <strong>Recurse<\/strong> parameter even when the <strong>Path<\/strong> value is an item without child-items (a &#8220;leaf&#8221;).<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-ChildItem -Path $PSHome\\*.ps1xml -Recurse<\/pre>\n<p>&nbsp;<\/p>\n<p>You can also use the <strong>Filter<\/strong> parameter. But neither take an array.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-ChildItem -Path $PSHome -Filter \"*.ps1xml\"\r\n\r\n<\/pre>\n<p>You can also use the <strong>Exclude<\/strong> parameter, which works without the &#8216;\\*&#8217; and Recurse, although it has the opposite effect. And, like <strong>Include<\/strong>, when you use the <strong>Recurse<\/strong> parameter with <strong>Exclude<\/strong>, the <strong>Depth<\/strong> parameter has no effect.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-ChildItem -Path $PSHome -Exclude \"*.ps1xml\"<\/pre>\n<p>&nbsp;<\/p>\n<p>And, you can pipe the result to Where-Object or the Where method, although those options are significantly slower.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-ChildItem -Path $PSHOME | Where-Object {$_.Extension -eq '.ps1xml' -or $_.Extension -eq '.dll'}<\/pre>\n<pre class=\"output\">PS C:\\&gt; (Get-ChildItem -Path $PSHOME).where({$_.Extension -eq '.ps1xml' -or $_.Extension -eq '.dll'})<\/pre>\n<p>&nbsp;<\/p>\n<h1>Read the help!<\/h1>\n<p>So, the moral of the story is to read the help. And, when the help is missing or wrong, file an issue on <a href=\"https:\/\/windowsserver.uservoice.com\/forums\/301869-powershell\/category\/148056-documentation\">UserVoice<\/a> or in <a href=\"https:\/\/github.com\/PowerShell\/PowerShell-Docs\/issues\/889\">the PowerShell-Docs repo on GitHub<\/a>. The cool folks on the PowerShell team, and their open-source helpers, respond very quickly.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image007.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-13319\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image007.png\" alt=\"\" width=\"424\" height=\"149\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image007.png 571w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2017\/01\/image007-300x106.png 300w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/a><\/p>\n<p>Like this Friday PowerShell Puzzle? <a href=\"https:\/\/www.sapien.com\/blog\/?s=Friday+puzzle\">Find more puzzles here<\/a>. If you have a PowerShell puzzle suggestion, post it here or ping me on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\">@juneb_get_help<\/a>.<\/p>\n<p><span class=\"blog-half-page\"><i>June Blender is a technology evangelist at SAPIEN Technologies, Inc and a Microsoft Cloud &amp; Datacenter MVP. You can reach her at <a href=\"mailto:juneb@sapien.com\">juneb@sapien.com<\/a> or follow her on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\">@juneb_get_help<\/a>.<\/i><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>On Friday, January 20, 2017, I posted the following puzzle on Twitter and Facebook (and here!). It shows a Get-ChildItem command where the value of the Path parameter is $PSHome, the Windows PowerShell installation directory, and the value of the Include parameter is &#8216;*ps1xml.&#8217; The command returns nothing &#8212; not a single file &#8212; even [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[932,2,1143,941,1182,25],"tags":[1187,934,28,1186],"class_list":["post-13315","post","type-post","status-publish","format-standard","hentry","category-beginners","category-general","category-powershell","category-powershell-5-0","category-puzzle","category-windows-powershell","tag-friday-puzzle","tag-juneb","tag-powershell","tag-puzzle"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/13315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=13315"}],"version-history":[{"count":13,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/13315\/revisions"}],"predecessor-version":[{"id":13332,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/13315\/revisions\/13332"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=13315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=13315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=13315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}