{"id":6426,"date":"2019-04-16T19:11:16","date_gmt":"2019-04-16T19:11:16","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6426"},"modified":"2019-04-16T19:11:16","modified_gmt":"2019-04-16T19:11:16","slug":"a-few-dir-ls-get-childitem-powershell-commands","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/a-few-dir-ls-get-childitem-powershell-commands\/","title":{"rendered":"A few dir\/ls\/Get-ChildItem Powershell commands"},"content":{"rendered":"<p>Get-ChildItem is aliased as ls\/dir (as well as gci), so is used to list files and directories. <\/p>\n<p>Here&#8217;s a few useful commands<\/p>\n<p><strong>Find only folders\/directories<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nls -Directory\r\n<\/pre>\n<p><strong>Find only files<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nls -File\r\n<\/pre>\n<p><strong>Find files with a given extension<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nls -Recurse | where {$_.Name -like &quot;*.bak&quot;}\r\n<\/pre>\n<p><strong>Only search to a certain depth<\/strong><\/p>\n<p>The above will recurse over all folders, but we might want to restrict this by a certain depth level, i.e. in this example up to 2 directories deep<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nls -Recurse -Depth 2 | where {$_.Name -like &quot;*.bak&quot;}\r\n<\/pre>\n<p><strong>Finding the directories with files with a given extension<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nls -Recurse -Depth 2 | where {$_.Name -like &quot;*.bak&quot;} | select directory -Unique\r\n<\/pre>\n<p>The use of -Unique ensure we do not have multiple directories with the same path\/name (i.e. for each .bak file found within a single directory)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get-ChildItem is aliased as ls\/dir (as well as gci), so is used to list files and directories. Here&#8217;s a few useful commands Find only folders\/directories ls -Directory Find only files ls -File Find files with a given extension ls -Recurse | where {$_.Name -like &quot;*.bak&quot;} Only search to a certain depth The above will recurse [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[118],"tags":[],"class_list":["post-6426","post","type-post","status-publish","format-standard","hentry","category-powershell"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6426"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6426\/revisions"}],"predecessor-version":[{"id":6430,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6426\/revisions\/6430"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}