{"id":254,"date":"2018-07-31T15:35:03","date_gmt":"2018-07-31T15:35:03","guid":{"rendered":"https:\/\/www.edrockwell.com\/blog\/?p=254"},"modified":"2026-07-02T16:57:33","modified_gmt":"2026-07-02T16:57:33","slug":"powershell-how-to-delete-files-and-folders-older-than-a-date","status":"publish","type":"post","link":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/","title":{"rendered":"Powershell &#8211; How to delete files and folders older than a date"},"content":{"rendered":"<p>Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick the script off. Actually, I&#8217;ve got a line commented out for you to run this script in &#8220;WhatIf&#8221; mode. In WhatIf mode, the script simply tells you what it would do if you ran it. Pretty cool!<\/p>\n<pre class=\"lang:default decode:true\">\r\n# Delete items older than a date including subfolders\r\n$directory = 'e:\\path\\to\\what\\you\\want\\to\\delete' \r\n$OlderThan = 365\r\n# To do a Whatif: Get-ChildItem $directory | Where-Object {$_.CreationTime -le (Get-Date).AddDays(-$OlderThan)} | Foreach-Object { Remove-Item $_.FullName -Recurse -Verbose -Force -whatif}\r\nGet-ChildItem $directory | Where-Object {$_.CreationTime -le (Get-Date).AddDays(-$OlderThan)} | Foreach-Object { Remove-Item $_.FullName -Recurse -Verbose -Force}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[107,108,21],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-delete-files-script","tag-delete-older-than","tag-powershell-2"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Ed Rockwell\"\/>\n\t<meta name=\"keywords\" content=\"delete files script,delete older than,powershell\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"System Admin [RESOLVED] | How To: Make My DevOps Life Easier\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]\" \/>\n\t\t<meta property=\"og:description\" content=\"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2018-07-31T15:35:03+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-02T16:57:33+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#article\",\"name\":\"Powershell \\u2013 How to delete files and folders older than a date | System Admin [RESOLVED]\",\"headline\":\"Powershell &#8211; How to delete files and folders older than a date\",\"author\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/#organization\"},\"datePublished\":\"2018-07-31T15:35:03+00:00\",\"dateModified\":\"2026-07-02T16:57:33+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#webpage\"},\"articleSection\":\"PowerShell, Delete Files Script, Delete Older Than, Powershell\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/category\\\/powershell\\\/#listItem\",\"name\":\"PowerShell\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/category\\\/powershell\\\/#listItem\",\"position\":2,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/category\\\/powershell\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#listItem\",\"name\":\"Powershell &#8211; How to delete files and folders older than a date\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#listItem\",\"position\":3,\"name\":\"Powershell &#8211; How to delete files and folders older than a date\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/category\\\/powershell\\\/#listItem\",\"name\":\"PowerShell\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/#organization\",\"name\":\"System Admin [RESOLVED]\",\"description\":\"How To: Make My DevOps Life Easier\",\"url\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Ed Rockwell\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Ed Rockwell\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#webpage\",\"url\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/\",\"name\":\"Powershell \\u2013 How to delete files and folders older than a date | System Admin [RESOLVED]\",\"description\":\"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/powershell-how-to-delete-files-and-folders-older-than-a-date\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2018-07-31T15:35:03+00:00\",\"dateModified\":\"2026-07-02T16:57:33+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/\",\"name\":\"System Admin [RESOLVED]\",\"description\":\"How To: Make My DevOps Life Easier\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.edrockwell.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]","description":"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick","canonical_url":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/","robots":"max-image-preview:large","keywords":"delete files script,delete older than,powershell","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#article","name":"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]","headline":"Powershell &#8211; How to delete files and folders older than a date","author":{"@id":"https:\/\/www.edrockwell.com\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/www.edrockwell.com\/blog\/#organization"},"datePublished":"2018-07-31T15:35:03+00:00","dateModified":"2026-07-02T16:57:33+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#webpage"},"isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#webpage"},"articleSection":"PowerShell, Delete Files Script, Delete Older Than, Powershell"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.edrockwell.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog\/category\/powershell\/#listItem","name":"PowerShell"}},{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog\/category\/powershell\/#listItem","position":2,"name":"PowerShell","item":"https:\/\/www.edrockwell.com\/blog\/category\/powershell\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#listItem","name":"Powershell &#8211; How to delete files and folders older than a date"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#listItem","position":3,"name":"Powershell &#8211; How to delete files and folders older than a date","previousItem":{"@type":"ListItem","@id":"https:\/\/www.edrockwell.com\/blog\/category\/powershell\/#listItem","name":"PowerShell"}}]},{"@type":"Organization","@id":"https:\/\/www.edrockwell.com\/blog\/#organization","name":"System Admin [RESOLVED]","description":"How To: Make My DevOps Life Easier","url":"https:\/\/www.edrockwell.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.edrockwell.com\/blog\/author\/admin\/#author","url":"https:\/\/www.edrockwell.com\/blog\/author\/admin\/","name":"Ed Rockwell","image":{"@type":"ImageObject","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","width":96,"height":96,"caption":"Ed Rockwell"}},{"@type":"WebPage","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#webpage","url":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/","name":"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]","description":"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/#breadcrumblist"},"author":{"@id":"https:\/\/www.edrockwell.com\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/www.edrockwell.com\/blog\/author\/admin\/#author"},"datePublished":"2018-07-31T15:35:03+00:00","dateModified":"2026-07-02T16:57:33+00:00"},{"@type":"WebSite","@id":"https:\/\/www.edrockwell.com\/blog\/#website","url":"https:\/\/www.edrockwell.com\/blog\/","name":"System Admin [RESOLVED]","description":"How To: Make My DevOps Life Easier","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.edrockwell.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"System Admin [RESOLVED] | How To: Make My DevOps Life Easier","og:type":"article","og:title":"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]","og:description":"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick","og:url":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/","article:published_time":"2018-07-31T15:35:03+00:00","article:modified_time":"2026-07-02T16:57:33+00:00","twitter:card":"summary","twitter:title":"Powershell \u2013 How to delete files and folders older than a date | System Admin [RESOLVED]","twitter:description":"Ok. so this seems pretty simple but sometimes can be daunting figuring out how to actually delete files and folders (recursively). In the script below, all you need to do is define the variables for the directory and how old do you want to go back. Simply change the $directory and $OlderThan variables and kick"},"aioseo_meta_data":{"post_id":"254","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-15 23:07:10","updated":"2026-07-02 17:28:24","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.edrockwell.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.edrockwell.com\/blog\/category\/powershell\/\" title=\"PowerShell\">PowerShell<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tPowershell \u2013 How to delete files and folders older than a date\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.edrockwell.com\/blog"},{"label":"PowerShell","link":"https:\/\/www.edrockwell.com\/blog\/category\/powershell\/"},{"label":"Powershell &#8211; How to delete files and folders older than a date","link":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-delete-files-and-folders-older-than-a-date\/"}],"_links":{"self":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":2,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/254\/revisions\/256"}],"wp:attachment":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}