Link to home
Start Free TrialLog in
Avatar of David Tolo Technology Manager
David Tolo Technology Manager

asked on

powershell script that deletes older file in a directory running as a service

I need a Windows PowerShell script that will delete all the files that are over 2 weeks old in a directory and all subdirectory without deleting the subdirectories.

I then need to convert it to a service so that it is running in the background.
ASKER CERTIFIED SOLUTION
Avatar of Timothy McCartney
Timothy McCartney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David Tolo Technology Manager
David Tolo Technology Manager

ASKER

Thanks for your quick response.  I have two questions here.

Where is the Directory specified?
I see that you have Write-Host "Couldn't remove $Item"
            Where does this write too?  Could this be set to write to a text file in the same folder?

If it is a scheduled task it would require no user intervention.

Also won't recurse make it delete the subfolders?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok.. I think that I almost understand it..

My only concern is that I will store the script in S:\Scripts

The files I want removed are in

S:\Backups

I am adding this as a scheduled tasks or as a service

Where can I add the logic for the script to run in s:\Backups\
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
whould this work?

param($ParentDirectory)
$ParentDirectory = "S:\PhoneBackup\"
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse

foreach ($Item in $Items){
    if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14))) {
        try {
            Remove-Item $Item.VersionInfo.FileName -Force
        }
        catch {
            "Couldn't remove $($Item) - Error Message: $($_)" | Out-File s:\PhoneBackup\FailedLog.txt -Append
            Write-Error $_
        }
    }
}
I think we posted at the same time :-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use this :)

param($ParentDirectory="S:\Backups")
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse

foreach ($Item in $Items){
    if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14))) {
        try {
            Remove-Item $Item.VersionInfo.FileName -Force
        }
        catch {
            Write-Host "Couldn't remove $Item"
            Write-Error $_
        }
    }
}

Open in new window

Thank you very much!!!
Happy to help! I want add that you should utilize my last snippet in your script since it sets the -ParentDirectory parameter automatically
yes.. I ended up with

param($ParentDirectory="S:\PhoneBackup")
$Items = Get-ChildItem -Path $ParentDirectory -File -Recurse

foreach ($Item in $Items){
    if ($Item.LastWriteTime -lt $((Get-date).AddDays(-14))) {
        try {
            Remove-Item $Item.VersionInfo.FileName -Force
        }
        catch {
            "Couldn't remove $($Item) - Error Message: $($_)" | Out-File s:\PhoneBackup\FailedLog.txt -Append
            Write-Error $_
        }
    }
}