{"id":76968,"date":"2023-07-12T13:53:24","date_gmt":"2023-07-12T11:53:24","guid":{"rendered":"https:\/\/jotelulu.com\/en-gb\/blog\/"},"modified":"2025-12-12T15:15:14","modified_gmt":"2025-12-12T14:15:14","slug":"delete-old-files-powershell","status":"publish","type":"post","link":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/","title":{"rendered":"Deleting old files using PowerShell scripts"},"content":{"rendered":"<p>Join us in this article to discover <strong>how to delete old files using PowerShell scripts<\/strong> that automatically remove files older than a specific creation date.<\/p>\n<p>System administrators often need to <strong>clean up directories<\/strong> like temp folders, log files, swap drives, and more. Unfortunately, manual review and sorting can be time-consuming and error-prone.<\/p>\n<p>In many cases, we need to <strong>delete files older than a certain age<\/strong>. This article provides a simple script you can run manually or integrate into a scheduled task that executes at regular intervals.<\/p>\n<h2><strong>How to delete old files using PowerShell scripts?<\/strong><\/h2>\n<p>The command structure is:<\/p>\n<pre><code>Get-ChildItem -Path \"&lt;PATH_TO_SCAN&gt;\" -Recurse | \r\nWhere-Object CreationTime -LT (Get-Date).AddDays(-&lt;DAYS&gt;) | \r\nRemove-Item<\/code><\/pre>\n<p>Where:<\/p>\n<ul>\n<li><strong><code>Get-ChildItem<\/code><\/strong>: Lists files and directories in a path.<\/li>\n<li><strong><code>-Path \"&lt;PATH_TO_SCAN&gt;\"<\/code><\/strong>: Defines the starting path for scanning.<\/li>\n<li><strong><code>-Recurse<\/code><\/strong>: Scans recursively through all subfolders.<\/li>\n<li><strong><code>|<\/code><\/strong>: Pipes output to the next command.<\/li>\n<li><strong><code>Where-Object CreationTime -LT (Get-Date).AddDays(-&lt;DAYS&gt;)<\/code><\/strong>: Filters files older than the specified number of days.<\/li>\n<li><strong><code>Remove-Item<\/code><\/strong>: Deletes matching files.<\/li>\n<\/ul>\n<p>Example: Delete files older than 7 days from <code>C:\\Drivers\\lj368\\<\/code>:<\/p>\n<pre><code>Get-ChildItem -Path \"C:\\Drivers\\lj368\\\" -Recurse | \r\nWhere-Object CreationTime -LT (Get-Date).AddDays(-7) | \r\nRemove-Item<\/code><\/pre>\n<p>This deletes files directly, but you might prefer to list them first or export to a file for review.<\/p>\n<figure class=\"wp-caption\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-19269\" src=\"https:\/\/jotelulu.com\/wp-content\/uploads\/2023\/07\/Imagen.-Revision-y-eliminacion-de-los-ficheros-mas-antiguos-mediante-PowerShell-1-1.jpg\" alt=\"Reviewing and deleting old files using PowerShell scripts\" width=\"1930\" height=\"1090\" \/><figcaption>Reviewing and deleting old files using PowerShell scripts<\/figcaption><\/figure>\n<h2 id=\"cleanup-script\"><strong>Quick cleanup: Delete old files with PowerShell<\/strong><\/h2>\n<p>If you just want to run it without the details, use this command:<\/p>\n<pre><code>Get-ChildItem -Path \"&lt;PATH_TO_SCAN&gt;\" -Recurse | \r\nWhere-Object CreationTime -LT (Get-Date).AddDays(-&lt;DAYS&gt;) | \r\nRemove-Item<\/code><\/pre>\n<p>Replace:<\/p>\n<ul>\n<li><strong><code>&lt;PATH_TO_SCAN&gt;<\/code><\/strong>: Your target folder path.<\/li>\n<li><strong><code>&lt;DAYS&gt;<\/code><\/strong>: Number of days (numeric).<\/li>\n<\/ul>\n<p>Run the one-liner and wait for completion.<\/p>\n<h2><strong>Conclusions<\/strong><\/h2>\n<p>As shown, <strong>deleting old files using PowerShell scripts<\/strong> lets you remove files older than a specified age efficiently. A few well-chained commands create a semi-automated solution (easily scheduled for full automation).<\/p>\n<p>This works on all current Microsoft Windows systems using basic PowerShell cmdlets. If you encounter issues, contact us\u2014we&#8217;re happy to help troubleshoot.<\/p>\n<p>Learn more PowerShell scripts and tips on our <a href=\"https:\/\/jotelulu.com\/en-gb\/blog\/\" target=\"_blank\" rel=\"noopener\">blog<\/a>.<\/p>\n<p><strong>Thanks for reading!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Join us in this article to discover how to delete old files using PowerShell scripts that automatically remove files older than a specific creation date. System administrators often need to clean up directories like temp folders, log files, swap drives, and more. Unfortunately, manual review and sorting can be time-consuming and error-prone. In many cases, [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":76970,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[287],"tags":[],"class_list":["post-76968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sysadmin-en-gb"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Deleting old files using PowerShell scripts - Jotelulu<\/title>\n<meta name=\"description\" content=\"Learn how to delete old files using PowerShell. One-line script removes files older than X days from any folder. Perfect for temp cleanup, logs, and automated maintenance.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deleting old files using PowerShell scripts - Jotelulu\" \/>\n<meta property=\"og:description\" content=\"Learn how to delete old files using PowerShell. One-line script removes files older than X days from any folder. Perfect for temp cleanup, logs, and automated maintenance.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"Jotelulu\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T11:53:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-12T14:15:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jotelulu.com\/wp-content\/uploads\/2025\/12\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1549\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Juan Ignacio Oller Aznar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Juan Ignacio Oller Aznar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/\"},\"author\":{\"name\":\"Juan Ignacio Oller Aznar\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/#\\\/schema\\\/person\\\/2ae97818cae086c48f108cd74234aca9\"},\"headline\":\"Deleting old files using PowerShell scripts\",\"datePublished\":\"2023-07-12T11:53:24+00:00\",\"dateModified\":\"2025-12-12T14:15:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/\"},\"wordCount\":279,\"image\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jotelulu.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg\",\"articleSection\":[\"Sysadmin\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/\",\"url\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/\",\"name\":\"Deleting old files using PowerShell scripts - Jotelulu\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jotelulu.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg\",\"datePublished\":\"2023-07-12T11:53:24+00:00\",\"dateModified\":\"2025-12-12T14:15:14+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/#\\\/schema\\\/person\\\/2ae97818cae086c48f108cd74234aca9\"},\"description\":\"Learn how to delete old files using PowerShell. One-line script removes files older than X days from any folder. Perfect for temp cleanup, logs, and automated maintenance.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jotelulu.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg\",\"contentUrl\":\"https:\\\/\\\/jotelulu.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg\",\"width\":2560,\"height\":1549},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/blog\\\/delete-old-files-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Portada\",\"item\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deleting old files using PowerShell scripts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/#website\",\"url\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/\",\"name\":\"Jotelulu\",\"description\":\"Cloud Paradise for Tech Companies\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/#\\\/schema\\\/person\\\/2ae97818cae086c48f108cd74234aca9\",\"name\":\"Juan Ignacio Oller Aznar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/87efd80399b2c40a1d78dd5cee4e788d3f6c29d33e3b67bcfbc6abbf91d0fdcf?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/87efd80399b2c40a1d78dd5cee4e788d3f6c29d33e3b67bcfbc6abbf91d0fdcf?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/87efd80399b2c40a1d78dd5cee4e788d3f6c29d33e3b67bcfbc6abbf91d0fdcf?s=96&d=mm&r=g\",\"caption\":\"Juan Ignacio Oller Aznar\"},\"url\":\"https:\\\/\\\/jotelulu.com\\\/en-gb\\\/author\\\/juanignaciooller\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deleting old files using PowerShell scripts - Jotelulu","description":"Learn how to delete old files using PowerShell. One-line script removes files older than X days from any folder. Perfect for temp cleanup, logs, and automated maintenance.","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:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/","og_locale":"en_GB","og_type":"article","og_title":"Deleting old files using PowerShell scripts - Jotelulu","og_description":"Learn how to delete old files using PowerShell. One-line script removes files older than X days from any folder. Perfect for temp cleanup, logs, and automated maintenance.","og_url":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/","og_site_name":"Jotelulu","article_published_time":"2023-07-12T11:53:24+00:00","article_modified_time":"2025-12-12T14:15:14+00:00","og_image":[{"width":2560,"height":1549,"url":"https:\/\/jotelulu.com\/wp-content\/uploads\/2025\/12\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg","type":"image\/jpeg"}],"author":"Juan Ignacio Oller Aznar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Juan Ignacio Oller Aznar","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#article","isPartOf":{"@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/"},"author":{"name":"Juan Ignacio Oller Aznar","@id":"https:\/\/jotelulu.com\/en-gb\/#\/schema\/person\/2ae97818cae086c48f108cd74234aca9"},"headline":"Deleting old files using PowerShell scripts","datePublished":"2023-07-12T11:53:24+00:00","dateModified":"2025-12-12T14:15:14+00:00","mainEntityOfPage":{"@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/"},"wordCount":279,"image":{"@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/jotelulu.com\/wp-content\/uploads\/2025\/12\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg","articleSection":["Sysadmin"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/","url":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/","name":"Deleting old files using PowerShell scripts - Jotelulu","isPartOf":{"@id":"https:\/\/jotelulu.com\/en-gb\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#primaryimage"},"image":{"@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#primaryimage"},"thumbnailUrl":"https:\/\/jotelulu.com\/wp-content\/uploads\/2025\/12\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg","datePublished":"2023-07-12T11:53:24+00:00","dateModified":"2025-12-12T14:15:14+00:00","author":{"@id":"https:\/\/jotelulu.com\/en-gb\/#\/schema\/person\/2ae97818cae086c48f108cd74234aca9"},"description":"Learn how to delete old files using PowerShell. One-line script removes files older than X days from any folder. Perfect for temp cleanup, logs, and automated maintenance.","breadcrumb":{"@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#primaryimage","url":"https:\/\/jotelulu.com\/wp-content\/uploads\/2025\/12\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg","contentUrl":"https:\/\/jotelulu.com\/wp-content\/uploads\/2025\/12\/Borrado-ficheros-antiguos-script-powershell-scaled-2.jpg","width":2560,"height":1549},{"@type":"BreadcrumbList","@id":"https:\/\/jotelulu.com\/en-gb\/blog\/delete-old-files-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Portada","item":"https:\/\/jotelulu.com\/en-gb\/"},{"@type":"ListItem","position":2,"name":"Deleting old files using PowerShell scripts"}]},{"@type":"WebSite","@id":"https:\/\/jotelulu.com\/en-gb\/#website","url":"https:\/\/jotelulu.com\/en-gb\/","name":"Jotelulu","description":"Cloud Paradise for Tech Companies","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jotelulu.com\/en-gb\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/jotelulu.com\/en-gb\/#\/schema\/person\/2ae97818cae086c48f108cd74234aca9","name":"Juan Ignacio Oller Aznar","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/87efd80399b2c40a1d78dd5cee4e788d3f6c29d33e3b67bcfbc6abbf91d0fdcf?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/87efd80399b2c40a1d78dd5cee4e788d3f6c29d33e3b67bcfbc6abbf91d0fdcf?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/87efd80399b2c40a1d78dd5cee4e788d3f6c29d33e3b67bcfbc6abbf91d0fdcf?s=96&d=mm&r=g","caption":"Juan Ignacio Oller Aznar"},"url":"https:\/\/jotelulu.com\/en-gb\/author\/juanignaciooller\/"}]}},"_links":{"self":[{"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/posts\/76968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/comments?post=76968"}],"version-history":[{"count":2,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/posts\/76968\/revisions"}],"predecessor-version":[{"id":77050,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/posts\/76968\/revisions\/77050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/media\/76970"}],"wp:attachment":[{"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/media?parent=76968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/categories?post=76968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jotelulu.com\/en-gb\/wp-json\/wp\/v2\/tags?post=76968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}