Thursday, February 28, 2013

Removing old folders with powershell

In attempt to automate mundane tasks I have put together some scripts to schedule on the server. Here is one of them I am planning on using to remove old snapshots of a SQL Server database that are more than 45 days old:
$initpath = "C:\Snapshots"
get-childitem -Path $initpath | where-object {$_.lastwritetime -lt (get-date).addDays(-45)} |
Foreach-Object { 
    if($_.Attributes -eq "Directory") {
        if (Test-Path $_.FullName) {
            Remove-Item -r $_.FullName


No comments:

Post a Comment