David Stuart wrote:
| Quote: |  | |
|
> Hello,
>
> I have a very deeply nested directory structure which is part of a
> loadbuild system. Because our loadbuild machine periodically runs out of
> space, I would like to run a scheduled task which deletes old
> build artifacts.
>
> So, how can I make a powershell script which deletes (recursively)
> directories which are older than X days? I'm not that familiar with the
> .NET APIs, which is (I think) why I'm running into trouble on this one.
>
> Thanks for the help. I imagine this is pretty easy for powershell veterans.
| |
| | |
Here's how I list all directories older than 2008 starting in c:\program
files:
PS C:\Program Files> get-childitem . -rec| `
where-object{$_.psiscontainer -and ($_.lastwritetime -lt "01/01/2008")}
Now to actually delete (just add this to the above command):
|foreach-object{remove-item $_ -recurse -whatif}
-whatif so you can check out what would be deleted. You can do a
start-transcript, run the above with the -whatif, stop-transcript, then
review the log of what should be deleted to make sure if meets your
requirements.
Marco
--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp
PowerGadgets MVP
http://www.powergadgets.com/mvp
Blog:
http://marcoshaw.blogspot.com