PowerShell.com

Join PowerShell.com!
PowerShell eBook
Script Library
Video Library
Meet Dr. Tobias Weltner


PowerShell eBook
Sign up for
Your PowerTip of the Day:

PowerShellPlus


Membership Privileges

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Here you'll find resources dedicated to helping you learn and master PowerShell along with forums for communicating with your PowerShell Peers. Here is a quick overview of all that PowerShell.com provides:

Join the PowerShell.com Community today!
Expert PowerShell advice and commentary from Dr. Tobias Weltner.
PowerShell script libraries with pre-populated and community posted scripts.
eBook - Mastering PowerShell. Written by Dr. Tobias Weltner.
Daily Tips on Windows PowerShell
PowerShell Community Forums
PowerShell Community Blogs
Popular RSS Feeds for PowerShell.com

Tobias' Tip of the Day

Finding Old Files

Occasionally, you might want to find files that are older than a give number of days to delete or backup those. A simple filter can provide that functionality:

filter FileAge($days) { if ( ($_.CreationTime -le (Get-Date).AddDays($days * -1) )) { $_ } }

Pipe the result of a Dir into FileAge filter, and it will only let those files pass that are at least the specified number of days old. The following line finds all PowerShell Script files in your personal folder that are at least 10 days old:

Dir $home\*.ps1 | FileAge 10

You could easily delete or backup the resulting files like this:

Dir $home\*.ps1 | FileAge 10 | Del -WhatIf

More Power Tips...

Copyright 2008 PowerShell.com. All rights reserved.