{"id":753,"date":"2015-09-06T23:09:07","date_gmt":"2015-09-06T21:09:07","guid":{"rendered":"http:\/\/www.netexpertise.eu\/en\/?p=753"},"modified":"2021-08-08T22:45:10","modified_gmt":"2021-08-08T20:45:10","slug":"remove-files-older-than","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html","title":{"rendered":"Delete Files Older Than in DOS \/ Powershell"},"content":{"rendered":"\n<p>System admins face more and more ever-growing directories storing auto-generated reports by scripts or scheduled tasks. Same goes to temp directories and nobody bother cleaning them up. But isn&#8217;t it our job too?<br>Here are 2 simple scripts that can be run every so often to delete files older than say 30 days for instance in Powershell and DOS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Remove Files in DOS<\/h2>\n\n\n\n<p>The first in DOS is more limited since ForFiles can only deal with the last modified date.<br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Echo @off\nCls\n\nSet Folder=C:\\Reports\n\nif exists %Folder% (\n  rem *******************************************\n  rem Remove system attributes and cache files\n  rem Not processed by the del command\n  Forfiles \/S \/P \"%Folder%\" \/M * \/D -30 \/C \"cmd \/c attrib -s -h @path\"\n  rem Remove files older than 30 days\n  Forfiles \/S \/P \"%Folder%\" \/M * \/D -30 \/C \"cmd \/c del \/F \/Q @path\"\n\n  rem Remove empty directories\n  for \/f \"delims=\" %%d in ('dir \/S \/B \/AD %SrcDir% ^| sort \/R') do rmdir \"%%d\"\n)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Delete Files in Powershell<\/h2>\n\n\n\n<p><a href=\"\/en\/tag\/powershell\">Powershell<\/a> lets you work with the 3 parameters LastAccessTime, LastWriteTime and CreationTime.<br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$limit = (Get-Date).AddDays(-30)\n$path = \"C:\\Temp\"\n\nif (Test-Path $path) {\n  # Delete files older than $limit days.\n  Get-ChildItem -Path $path -Recurse | \n  Where-Object { !$_.PSIsContainer -and $_.lastAccessTime -lt $limit } |\n  Remove-Item -Force\n\n  # Delete any empty directories left behind after deleting old files.\n  Get-ChildItem -Path $path -Recurse |\n  Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse |\n  Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse\n}<\/code><\/pre>\n\n\n\n<p><br>To run the script, check powershell restrictions with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-ExecutionPolicy<\/code><\/pre>\n\n\n\n<p>If restricted, run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Set-ExecutionPolicy RemoteSigned<\/code><\/pre>\n\n\n\n<p>And run the following command in a schedule task to delete files on a regular basis:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">powershell C:\\Scripts\\ClearFolder.ps1<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>System admins face more and more ever-growing directories storing auto-generated reports by scripts or scheduled tasks. Same goes to temp directories and nobody bother cleaning them up. But isn&#8217;t it our job too?Here are 2 simple scripts that can be run every so often to delete files older than say 30 days for instance in [&hellip;]<\/p>\n","protected":false},"author":2,"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},"categories":[70],"tags":[256,383],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Delete Files Older Than in DOS \/ Powershell<\/title>\n<meta name=\"description\" content=\"Delete \/ remove files older than a certain number of days in DOS and Windows Powershell\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Delete Files Older Than in DOS \/ Powershell\" \/>\n<meta property=\"og:description\" content=\"Delete \/ remove files older than a certain number of days in DOS and Windows Powershell\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-06T21:09:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-08T20:45:10+00:00\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html\",\"url\":\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html\",\"name\":\"Netexpertise - Delete Files Older Than in DOS \/ Powershell\",\"isPartOf\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2015-09-06T21:09:07+00:00\",\"dateModified\":\"2021-08-08T20:45:10+00:00\",\"author\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\"Delete \/ remove files older than a certain number of days in DOS and Windows Powershell\",\"breadcrumb\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Delete Files Older Than in DOS \/ Powershell\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"http:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Delete Files Older Than in DOS \/ Powershell","description":"Delete \/ remove files older than a certain number of days in DOS and Windows Powershell","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":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Delete Files Older Than in DOS \/ Powershell","og_description":"Delete \/ remove files older than a certain number of days in DOS and Windows Powershell","og_url":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html","og_site_name":"Netexpertise","article_published_time":"2015-09-06T21:09:07+00:00","article_modified_time":"2021-08-08T20:45:10+00:00","author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html","url":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html","name":"Netexpertise - Delete Files Older Than in DOS \/ Powershell","isPartOf":{"@id":"http:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2015-09-06T21:09:07+00:00","dateModified":"2021-08-08T20:45:10+00:00","author":{"@id":"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":"Delete \/ remove files older than a certain number of days in DOS and Windows Powershell","breadcrumb":{"@id":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.netexpertise.eu\/en\/systems\/windows\/remove-files-older-than.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Delete Files Older Than in DOS \/ Powershell"}]},{"@type":"WebSite","@id":"http:\/\/www.netexpertise.eu\/en\/#website","url":"http:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/753"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}