{"id":8622,"date":"2021-10-14T11:25:05","date_gmt":"2021-10-14T15:25:05","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8622"},"modified":"2021-10-14T11:25:09","modified_gmt":"2021-10-14T15:25:09","slug":"finding-modified-files-with-powershell","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/","title":{"rendered":"Finding Modified Files with PowerShell"},"content":{"rendered":"\n<p>Here's another task that I seem to be constantly fiddling with using PowerShell. What files did I work on yesterday? Or what files were modified in the last 48 hours? Obviously, Get-ChildItem is going to be the primary command. It is simple enough to get files based on an extension from a given folder path, even recursively. But to filter on age, I have to use Where-Object and compare, in this scenario the LastWriteTime property, to a DateTime value. By the way, I am in the PowerShell Working Group focused on cmdlets and there was a request to add DateTime filtering to Get-ChildItem. Unfortunately, the .NET Framework doesn't provide an early filtering mechanism for created or modified dates. Any filtering would have to be done after Get-ChildItem returned all the files, which is no different than using Where-Object. The end result would be a lot of work on Get-ChildItem with no performance gain so this feature won't be implemented. But that's ok. I have a function for that!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get-LastModifiedFile<\/h2>\n\n\n\n<p>This function should work in both Windows PowerShell 5.1 and PowerShell 7.x. Here's the full file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">#requires -version 5.1\nFunction Get-LastModifiedFile {\n    [cmdletbinding()]\n    [alias(\"glm\")]\n    [OutputType(\"System.IO.FileInfo\")]\n    Param(\n        [Parameter(Position = 0, HelpMessage = \"Specify a file filter like *.ps1.\")]\n        [ValidateNotNullOrEmpty()]\n        [string]$Filter = \"*\",\n\n        [Parameter(Position = 1, HelpMessage = \"Specify the folder to search.\")]\n        [ValidateScript({\n          #this will write a custom error message if validation fails\n          If ((Test-Path -path $_ -PathType Container) -AND ((Get-Item -path $_).psprovider.name -eq 'Filesystem')) {\n              return $True\n          }\n          else {\n              Throw \"The specified Path value $_ is not a valid folder or not a file system location.\"\n              return $False\n          }\n        })]\n        [string]$Path = \".\",\n\n        [Parameter(HelpMessage = \"Specify the search interval based on the last write time.\")]\n        [ValidateSet(\"Hours\", \"Minutes\", \"Days\", \"Months\", \"Years\")]\n        [string]$Interval = \"Hours\",\n\n        [Parameter(HelpMessage = \"Specify the number of intervals.\")]\n        [alias(\"ic\")]\n        [ValidateScript({$_ -ge 1})]\n        [int32]$IntervalCount = 24,\n\n        [Parameter(HelpMessage = \"Recurse from the specified path.\")]\n        [switch]$Recurse\n    )\n\n    Write-Verbose \"Starting $($myinvocation.mycommand)\"\n    $msg =\"Searching {0} for {1} files modified in the last {2} {3}.\" -f (Convert-Path $Path),$filter,$IntervalCount,$Interval\n    Write-Verbose $msg\n\n    switch ($Interval) {\n        \"minutes\" { $last = (Get-Date).AddMinutes(-$IntervalCount) }\n        \"hours\" { $last = (Get-Date).AddHours(-$IntervalCount) }\n        \"days\" { $last = (Get-Date).AddDays(-$IntervalCount) }\n        \"months\" { $last = (Get-Date).AddMonths(-$IntervalCount) }\n        \"years\" { $last = (Get-Date).AddYears(-$IntervalCount) }\n    }\n    Write-Verbose \"Cutoff date is $Last\"\n\n    #remove bound parameters that don't belong to Get-ChildItem\n    \"IntervalCount\", \"Interval\" | ForEach-Object {\n        if ($PSBoundParameters.ContainsKey($_)) {\n            [void]$PSBoundParameters.Remove($_)\n        }\n    }\n    #add -File to PSBoundParameters\n    $PSBoundParameters.Add(\"file\", $True)\n    if ($recurse) {\n        Write-Verbose \"Recursing...\"\n    }\n    else {\n        Write-Verbose \"Searching...\"\n    }\n\n    #get the files and filter on the LastWriteTime using the Where() method for\n    #better performance\n    (Get-ChildItem @PSBoundParameters).Where({$_.LastWriteTime -ge $last})\n\n    Write-Verbose \"Ending $($myinvocation.mycommand)\"\n}\n<\/code><\/pre>\n\n\n\n<p>I've added comments and help messages throughout the code so you should be able to understand what I'm doing. But I want to point out a few things.<\/p>\n\n\n\n<p>First, I am using a ValidateScript() attribute on the Path parameter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">If ((Test-Path -path $_ -PathType Container) -AND ((Get-Item -path $_).psprovider.name -eq 'Filesystem')) {\n    return $True\n}\nelse {\n    Throw \"The specified Path value $_ is not a valid folder or not a file system location.\"\n    return $False\n}<\/code><\/pre>\n\n\n\n<p>I am testing that the $Path value is a container and that it is a FileSystem object. I could have simply used the IF condition, but I wanted to have more control over the error message if validation failed. The code should be pretty clear. Here's what it produces.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"170\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-1024x170.png\" alt=\"\" class=\"wp-image-8623\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-1024x170.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-300x50.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-768x128.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-850x141.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error.png 1095w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>This is from PowerShell 7. Windows PowerShell also displays the custom error message plus a bit more of the exception.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Splatting PSBoundParameters<\/h2>\n\n\n\n<p>Another feature in my function is the re-use of PSBoundparameters. Because almost all of the parameters are the same as Get-ChildItem, I can splat $PSBoundParameters. However, I need to remove invalid parameters IntervalCount and Interval.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">\"IntervalCount\", \"Interval\" | ForEach-Object {\n    if ($PSBoundParameters.ContainsKey($_)) {\n        [void]$PSBoundParameters.Remove($_)\n    }\n}<\/code><\/pre>\n\n\n\n<p>I also want to add the Get-ChildItem File parameter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$PSBoundParameters.Add(\"file\", $True)<\/code><\/pre>\n\n\n\n<p>Technically, PowerShell lets you splat AND specify parameters but I wanted to keep my code clean. The parameters that have default values like Filter and Path won't be detected as PSBoundParameters unless I specify a different value. But that's ok because the default values are the same as Get-ChildItem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Switch the Interval<\/h2>\n\n\n\n<p>My function uses a ValidateSet() attribute for the Interval and I am using a default value of Hours. When setting a default, make sure it belongs to your validation set. This ensures that I have a known value that I can use in a Switch statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">switch ($Interval) {\n    \"minutes\" { $last = (Get-Date).AddMinutes(-$IntervalCount) }\n    \"hours\"   { $last = (Get-Date).AddHours(-$IntervalCount) }\n    \"days\"    { $last = (Get-Date).AddDays(-$IntervalCount) }\n    \"months\"  { $last = (Get-Date).AddMonths(-$IntervalCount) }\n    \"years\"   { $last = (Get-Date).AddYears(-$IntervalCount) }\n}<\/code><\/pre>\n\n\n\n<p>Because I have multiple possible values, this is a cleaner approach than using a more complicated If\/ElseIf\/ElseIf\/Else statement. The Switch statement produces a DateTime value that serves as my cutoff date. I should also point out that the dash before $IntervalCount is not indicating that it is a parameter. It is the minus sign. I am adding a negative value from $IntervalCount.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Where() Method<\/h2>\n\n\n\n<p>I use the $Last value in my final filtering.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">(Get-ChildItem @PSBoundParameters).Where({$_.LastWriteTime -ge $last})<\/code><\/pre>\n\n\n\n<p>This code may look a bit different to you. I am using the Where() method and not the Where-Object cmdlet. I could have used this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-ChildItem @PSBoundParameters | Where-Object {$_.LastWriteTime -ge $last}<\/code><\/pre>\n\n\n\n<p>And get the same result. Where() is a built-in method on collections or arrays. That's why the Get-ChildItem expression is wrapped in parentheses. The syntax in the Where() method is the same as Where-Object. The difference is performance.  Usually, the method is faster than using the cmdlet. In my testing, we're talking milliseconds difference, but faster is faster so I'll go with it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm.png\"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"292\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm.png\" alt=\"\" class=\"wp-image-8624\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm.png 711w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-300x123.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"606\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm2.png\" alt=\"\" class=\"wp-image-8625\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm2.png 770w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm2-300x236.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm2-768x604.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/a><\/figure>\n\n\n\n<p>The function output is the filtered Get-ChildItem objects to the pipeline so I can use it any way I need.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/extension-report.png\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"316\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/extension-report.png\" alt=\"\" class=\"wp-image-8626\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/extension-report.png 660w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/extension-report-300x144.png 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><\/figure>\n\n\n\n<p>These are the file types I have worked on in the last 6 months. Those 153 files with no extensions are from an unhidden .git folder which I'll have to fix.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>This is why I love PowerShell. With a little effort, I created a command-line tool I can quickly run at the console to find what I need. I even defined an alias, <em>glm<\/em>, to make it even easier to use at a command prompt. I hope you picked up a tip or two that you can use in your own PowerShell scripting. To learn more about parameter validation options, read the About_Functions_Advanced_Parameters help topic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s another task that I seem to be constantly fiddling with using PowerShell. What files did I work on yesterday? Or what files were modified in the last 48 hours? Obviously, Get-ChildItem is going to be the primary command. It is simple enough to get files based on an extension from a given folder path,&#8230;<\/p>\n","protected":false},"author":1,"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,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Finding Modified Files with #PowerShell","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[224,137,540],"class_list":["post-8622","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-function","tag-get-childitem","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Finding Modified Files with PowerShell &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"Here&#039;s how I built a simple PowerShell function to quickly find files modified within a given interval like the last 24 hours or 6 months.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Finding Modified Files with PowerShell &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Here&#039;s how I built a simple PowerShell function to quickly find files modified within a given interval like the last 24 hours or 6 months.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-14T15:25:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-14T15:25:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-1024x170.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Finding Modified Files with PowerShell\",\"datePublished\":\"2021-10-14T15:25:05+00:00\",\"dateModified\":\"2021-10-14T15:25:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/\"},\"wordCount\":746,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/glm-error-1024x170.png\",\"keywords\":[\"Function\",\"Get-ChildItem\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/\",\"name\":\"Finding Modified Files with PowerShell &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/glm-error-1024x170.png\",\"datePublished\":\"2021-10-14T15:25:05+00:00\",\"dateModified\":\"2021-10-14T15:25:09+00:00\",\"description\":\"Here's how I built a simple PowerShell function to quickly find files modified within a given interval like the last 24 hours or 6 months.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/glm-error.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/glm-error.png\",\"width\":1095,\"height\":182},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8622\\\/finding-modified-files-with-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Finding Modified Files with PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Finding Modified Files with PowerShell &#8226; The Lonely Administrator","description":"Here's how I built a simple PowerShell function to quickly find files modified within a given interval like the last 24 hours or 6 months.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/","og_locale":"en_US","og_type":"article","og_title":"Finding Modified Files with PowerShell &#8226; The Lonely Administrator","og_description":"Here's how I built a simple PowerShell function to quickly find files modified within a given interval like the last 24 hours or 6 months.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-10-14T15:25:05+00:00","article_modified_time":"2021-10-14T15:25:09+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-1024x170.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Finding Modified Files with PowerShell","datePublished":"2021-10-14T15:25:05+00:00","dateModified":"2021-10-14T15:25:09+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/"},"wordCount":746,"commentCount":0,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-1024x170.png","keywords":["Function","Get-ChildItem","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/","name":"Finding Modified Files with PowerShell &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error-1024x170.png","datePublished":"2021-10-14T15:25:05+00:00","dateModified":"2021-10-14T15:25:09+00:00","description":"Here's how I built a simple PowerShell function to quickly find files modified within a given interval like the last 24 hours or 6 months.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/glm-error.png","width":1095,"height":182},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8622\/finding-modified-files-with-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Finding Modified Files with PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3551,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-3-0\/3551\/powershell-clean-up-tools\/","url_meta":{"origin":8622,"position":0},"title":"PowerShell Clean Up Tools","author":"Jeffery Hicks","date":"November 11, 2013","format":false,"excerpt":"A few years ago I think I posted some PowerShell clean up tools. These were functions designed to help clear out old files, especially for folders like TEMP. Recently I decided to upgrade them to at least PowerShell 3.0 to take advantage of v3 cmdlets and features. I use these\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"021913_2047_WordTest1.png","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/02\/021913_2047_WordTest1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":9057,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/9057\/using-powershell-your-way\/","url_meta":{"origin":8622,"position":1},"title":"Using PowerShell Your Way","author":"Jeffery Hicks","date":"June 6, 2022","format":false,"excerpt":"I've often told people that I spend my day in a PowerShell prompt. I run almost my entire day with PowerShell. I've shared many of the tools I use daily on Github. Today, I want to share another way I have PowerShell work the way I need it, with minimal\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/06\/dl.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/06\/dl.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/06\/dl.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2022\/06\/dl.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2673,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2673\/friday-fun-edit-recent-file\/","url_meta":{"origin":8622,"position":2},"title":"Friday Fun: Edit Recent File","author":"Jeffery Hicks","date":"January 4, 2013","format":false,"excerpt":"As you might imagine I work on a lot of PowerShell projects at the same time. Sometimes I'll start something at the beginning of the week and then need to come back to it at the end of the week. The problem is that I can't always remembered what I\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"Edit-RecentFile","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/01\/Edit-RecentFile-300x209.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":510,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/510\/all-hail-dir-usealot\/","url_meta":{"origin":8622,"position":3},"title":"All Hail Dir UseALot!","author":"Jeffery Hicks","date":"November 16, 2009","format":false,"excerpt":"Some of you know my relationship with the a command prompt goes back a long, long way. Naturally I became very adept at using the DIR command, fully taking advantage of its switches to tease out hidden information or to quickly get just the information I wanted. When PowerShell first\u2026","rel":"","context":"In &quot;CommandLine&quot;","block_context":{"text":"CommandLine","link":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},"img":{"alt_text":"captured_Image.png","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2009\/11\/captured_Image.png_thumb.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":7549,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7549\/building-a-powershell-inventory\/","url_meta":{"origin":8622,"position":4},"title":"Building a PowerShell Inventory","author":"Jeffery Hicks","date":"June 16, 2020","format":false,"excerpt":"A few weeks ago, a new Iron Scripter PowerShell scripting challenge was issued. For this challenge we were asked to write some PowerShell code that we could use to inventory our PowerShell script library.\u00a0 Here's how I approached the problem, which by no means is the only way. Lines of\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/ast-results.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/ast-results.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/ast-results.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":8593,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8593\/theres-a-file-in-my-powershell-bucket\/","url_meta":{"origin":8622,"position":5},"title":"There&#8217;s a File in My PowerShell Bucket","author":"Jeffery Hicks","date":"September 28, 2021","format":false,"excerpt":"If there's one task I've never stopped doing, it is finding files. I am constantly creating new ways to organize files and display them in a meaningful format. Naturally, PowerShell is a great tool for this task. Get-ChildItem is obviously the proper starting point. The cmdlet works fine in getting\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/fileage3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/fileage3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/fileage3.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/fileage3.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8622"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8622\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}