Powershell Query

Matthew Hinchliffe
Matthew Hinchliffe used Ask the Experts™
on
Hi

I have a Powershell that I've created to delete some Spiceworks backups.  Basically I want it to delete anything older than 4 days.  Initially I used CreationTime but I realised this didnt work if I copied the files to another location.  So I changed it to LastWriteTime.

When I run the script it correctly outputs items Older than 4 days and items Last than 4 days old.  I don't get any errors.

The problem is that it doesnt actually delete the items in the folder and I can't see where I've gone wrong.  I created the script in PowerShell  ISE and have run it from there and also from the Powershell command line (as administrator).

I am currently running this locally from c:\ as I want to have it working and fully understand it before setting up as a scheduled task.

The script:

# Defines the '4 days old' (today's date minus 4)
$age = (Get-Date).AddDays(-4)

# Get all the files in the folder and subfolders | foreach file
Get-ChildItem c:\spiceworksbackups -Recurse -File | foreach{

    # if LastWriteTime is 'le' (less or equal) than 4 days
    if ($_.LastWriteTime -le $age){

        Write-Output "Older than 4 days - $($_.name)"

        # remove the item

        Remove-Item $_.fullname -Force -Verbose

    }else{

        Write-Output "Less than 4 days old - $($_.name)"

        # Do stuff
    }
}

The Output:
Older than 4 days - spiceworks-backup-[V7.5.00101]-[2018-09-01].zip
Older than 4 days - spiceworks-backup-[V7.5.00101]-[2018-09-02].zip
Older than 4 days - spiceworks-backup-[V7.5.00101]-[2018-09-03].zip
Less than 30 days old - spiceworks-backup-[V7.5.00101]-[2018-09-04].zip
Less than 30 days old - spiceworks-backup-[V7.5.00101]-[2018-09-05].zip
Less than 30 days old - spiceworks-backup-[V7.5.00101]-[2018-09-06].zip
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
Remove-Item $_.fullname
That's your issue.

Surely that should be         Remove-Item $_.name
Michael B. SmithManaging Consultant

Commented:
It works here just fine (although I put a "-whatif" on remove-item here so nothing got removed).

Are you sure you have permissions to remove the files?
Matthew HinchliffeIT Engineer

Author

Commented:
Hi Fella's

I've changed it to Remove-Item $_.name but still getting the same problem.

I'm running the script as administrator under an account which is a member of the local admins group on my pc.  

I can manually delete the files without any problem.

I must be missing something really obvious...
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Senior Infrastructure Analyst
Commented:
limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

Open in new window


try that, modify your $Path, used this before and it does work.
Matthew HinchliffeIT Engineer

Author

Commented:
Thanks for the help guys.

Not sure what I had got wrong on the original one, but I managed to get it working based on the last example so thanks for that.
AlexSenior Infrastructure Analyst

Commented:
You're very welcome, any further issues you know where we are :D

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial