{"id":8551,"date":"2012-07-31T00:01:00","date_gmt":"2012-07-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/07\/31\/use-powershell-to-help-find-all-of-your-images\/"},"modified":"2012-07-31T00:01:00","modified_gmt":"2012-07-31T00:01:00","slug":"use-powershell-to-help-find-all-of-your-images","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-help-find-all-of-your-images\/","title":{"rendered":"Use PowerShell to Help Find All of Your Images"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to help locate pictures or images on the hard drive.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked a little bit about review time at Microsoft. We have to count everything so that we can provide our managers with feedback. Luckily, Windows PowerShell has the <b>Measure-Object<\/b> cmdlet, so it is pretty easy. Windows PowerShell also works with dates really easily, so it is simple enough to write a script to find all images with a file creation date between July 1, 2011 and June 30, 2012. If you have your hard disk drive organized in such a way that only work related pictures or images reside when you do the search, dude, you are golden.<\/p>\n<p>For me, it is easy to find all of the images or all of the articles I wrote this year. This is because of how my <b>Data<\/b><i> <\/i>folder is laid out. I can view the high-level layout by using the <b>Get-ChildItem<\/b> cmdlet without the <b>recurse<\/b> option and filtering for the directories. The Windows PowerShell&nbsp;2.0 command is shown here.<\/p>\n<p style=\"padding-left: 30px\">dir C:\\data\\ScriptingGuys | where {$_.PsIsContainer}<\/p>\n<p>By using the simple Windows PowerShell 3.0 syntax, the command becomes the following.<\/p>\n<p style=\"padding-left: 30px\">dir C:\\data\\ScriptingGuys | where PsIsContainer<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5037.HSG-7-31-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5037.HSG-7-31-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Simplify things by using a PS Drive<\/h2>\n<p>Because the folder layout is so structured, it is easy to find items on a year-by-year basis. For example, I can create a custom PS Drive that is rooted in 2012, and my commands are vastly simplified. The first thing I do is store the current location on the stack by using <b>pushd<\/b> (alias for <b>Push-Location)<\/b>. Next, I create a new Windows PowerShell drive that is rooted in the 2012 folder. This will simplify my command line and make it easier to work. Now I change to that location (<b>sl<\/b> is an alias for <b>Set-Location<\/b>) and finally I use <b>dir<\/b> (an alias for <b>Get-ChildItem<\/b>) to find all of the .jpg, .png and .bmp types of files. To count them all, I use the <b>Measure-Object<\/b> cmdlet (<b>measure<\/b> is an alias). The commands is shown here.<\/p>\n<p style=\"padding-left: 30px\">pushd<\/p>\n<p style=\"padding-left: 30px\">New-PSDrive -Root C:\\data\\ScriptingGuys\\2012 -PSProvider filesystem -Name hsg<\/p>\n<p style=\"padding-left: 30px\">sl hsg:<\/p>\n<p style=\"padding-left: 30px\">dir -Recurse -include *png,*jpg,*bmp | measure<\/p>\n<p>The commands and the output associated with the commands are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6012.HSG-7-31-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6012.HSG-7-31-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If there are 738 images, how many documents have there been this year? A quick change of the <b>Include<\/b><i> <\/i>parameter to <b>*doc<\/b> and <b>*docx<\/b> finds that there have been nearly 300 documents this year.<\/p>\n<p style=\"padding-left: 30px\">PS hsg:\\&gt; dir -Recurse -include *doc,*docx | measure<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 296<\/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 :&nbsp;<\/p>\n<p>Wow! 296 articles have been created. But how many days have there been so far this year? Well, that information is always available from the <b>Get-Date<\/b> cmdlet, as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS hsg:\\&gt; (Get-Date).DayOfYear<\/p>\n<p style=\"padding-left: 30px\">209<\/p>\n<p>The difference between the number of Word documents and the number of days is that on many occasions, the Hey, Scripting Guy! Blog publishes more than once a day. For example, during the Scripting Games and during TechEd this year, it was not uncommon to have four or even five postings a day. What is interesting is that I am running an average of 2.5 images per blog this calendar year.<\/p>\n<h2>Finding images created during a specific time range<\/h2>\n<p>To find images that I created during the time range of July 1, 2011 and June 30, 2012, I need to search multiple folders. This is because I organized my folders by calendar year, not by fiscal year. Therefore, I need to back up a bit in the folder structure. For me, I will back up to the root of the ScriptingGuys folder and begin my search there. The command is a one-liner, and at first I let it run and display all of the images so I can confirm that the command works properly. I decide to sort the results to make it obvious (otherwise the dates begin with October and go sideways in chronology). The trick is to use the compound <b>Where-Object<\/b> (<b>where<\/b> is an alias), and the greater than (<b>-gt<\/b>) and less than or equal (<b>-le<\/b>) operators to provide only the dates desired. Here is the basic command.<\/p>\n<p style=\"padding-left: 30px\">dir -path C:\\data\\ScriptingGuys -Recurse -include *.png,*.jpg,*.bmp |<\/p>\n<p style=\"padding-left: 30px\">where {$_.LastWriteTime -gt [datetime]&#8221;7\/1\/11&#8243; -AND $_.lastwritetime -le [datetime]&#8221;6\/30\/12&#8243;} &nbsp;| &nbsp;<\/p>\n<p style=\"padding-left: 30px\">sort &nbsp;lastwritetime<\/p>\n<p>The command and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7827.HSG-7-31-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7827.HSG-7-31-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To count the number of images during the past fiscal year, all that I need to do is add the <b>Measure-Object<\/b> cmdlet. I do not need the <b>Sort-Object<\/b> cmdlet, so I remove it from the command. The revised command and associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir -path C:\\data\\ScriptingGuys -Recurse -include *.png,*.jpg,*.bmp |<\/p>\n<p style=\"padding-left: 30px\">where {$_.LastWriteTime -gt [datetime]&#8221;7\/1\/11&#8243; -AND $_.lastwritetime -le [datetime]&#8221;6\/30\/12&#8243;} | &nbsp;<\/p>\n<p style=\"padding-left: 30px\">Measure-Object<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 1228<\/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>Playing with Files Week will continue tomorrow.<\/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: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to help locate pictures or images on the hard drive. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked a little bit about review time at Microsoft. We have to count everything so that we can provide our managers with feedback. Luckily, Windows [&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-8551","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: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to help locate pictures or images on the hard drive. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked a little bit about review time at Microsoft. We have to count everything so that we can provide our managers with feedback. Luckily, Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8551","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=8551"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8551\/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=8551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}