Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The given answers will only delete files (which admittedly is what is in the title of this post), but here's some code that will first delete all of the files older than 15 days, and then recursively delete any empty directories that may have been left behind. My code also uses the <code>-Force</code> option to delete hidden and read-only files as well. Also, I chose to not use aliases as the OP is new to PowerShell and may not understand what <code>gci</code>, <code>?</code>, <code>%</code>, etc. are.</p> <pre><code>$limit = (Get-Date).AddDays(-15) $path = "C:\Some\Path" # Delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force # Delete any empty directories left behind after deleting the old files. Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse </code></pre> <p>And of course if you want to see what files/folders will be deleted before actually deleting them, you can just add the <code>-WhatIf</code> switch to the <code>Remove-Item</code> cmdlet call at the end of both lines.</p> <p>The code shown here is PowerShell v2.0 compatible, but I also show this code and the faster PowerShell v3.0 code as <a href="http://blog.danskingdom.com/powershell-functions-to-delete-old-files-and-empty-directories/" rel="noreferrer">handy reusable functions on my blog</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload