Remove files older than x days with PoSH


One of the Symantec Live Update Servers started to complain about low disk space. So I started Treesize Pro to see what files or folders where eating my disk space.  After a couple of seconds Treesize Pro showed me that the liveupdate files, in the c:\LiveUpdate folder where the problem.

I created the following Powershell Script (after reading the source) and scheduled this script to run once a week.

Warning: This script will remove everything that matches the remove-item cmdlet!

$Now = Get-Date
$Days = "3"
$TargetFolder = "C:\LiveUpdate"
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.zip, *.x86 -recurse `
    | Where {$_.LastWriteTime -le "$LastWrite"} 

foreach ($File in $Files)
{write-host "Deleting File $File" -foregroundcolor "Red"; `
    Remove-Item $File | out-null}

Just change the $Days, $TargetFolder variables and the file extensions (*.zip, *.x86) for you environment.
Source of this script: powershell-script-to-delete-files-older-than-certain-days.aspx


2 thoughts on “Remove files older than x days with PoSH

  1. Ivan Versluis

    Thanks Arne! I will use this ps script. I run into the same problem some weeks ago; my local contact in US complained the defs were not updated for some time. I will blog on my own site, but it makes sense for other guys to add the command how to schedule the job.

  2. Filan


    That is great script. but i dont know why it does not delete folders, it deletes only files 😦 is there a chance to do something that this scripts deletes also folders,

    best Regards


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s