Powershell Query

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
Matthew HinchliffeIT EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex Green3rd Line Server SupportCommented:
Remove-Item $_.fullname
That's your issue.

Surely that should be         Remove-Item $_.name
0
Michael B. SmithExchange & Active Directory ExpertCommented:
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?
0
Matthew HinchliffeIT EngineerAuthor 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...
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

Alex Green3rd Line Server SupportCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matthew HinchliffeIT EngineerAuthor 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.
1
Alex Green3rd Line Server SupportCommented:
You're very welcome, any further issues you know where we are :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.