We help IT Professionals succeed at work.

Delete files older than x (using PS)

samiam41
samiam41 asked
on
Hey Experts.  I'm trying to do something simple (or so I thought) and wanted to delete out old SQL backup files (*.diff and *.bak).  If I try to manually delete them, I have no problems.  When I try one of the 5 or 6 I found on the web and modified the days/dir path, I get an error:

Remove-Item : Cannot remove item E:\sql backups\WSS_Content_db_201910232200.diff: Access to the path 'E:\sql backups\WSS_Content_db_201910232200.diff' is denied.

I've tried:
$Path = “E:\SQL Backups”
$Daysback = “-45”
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Recurse

Open in new window


And:
$limit = (Get-Date).AddDays(-45)
$path = "E:\SQL Backups"

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

Open in new window


If you have a different script you'd like me to test, go for it.  I'm up for whatever in the heck will get these files deleted out of that dir without manual intervention.  Thank you in advance for your help and time on this!
Comment
Watch Question

Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
That's not related to the scripts, they should work just fine.
Well, the second one at least. The first one may have unintended consequences if there are subdirectories in E:\SQL Backups, because it will try to delete those as well, depending on the LastWriteTime.
Anyway, if PS says "Access is denied", it usually does mean "Access is denied".
That could be either because you actually don't have access (are you running the commands in an elevated PS console?), or that the file is currently in use (which, given the age, is somewhat unlikely).

Author

Commented:
Hey oBdA!  I thought the same things about why it isn't running.  I checked the NTFS permissions on the dir where the files are and administrators have FC.  I can delete them out if I right-click each file and choose delete.  No problems.
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Still: is the PS console you're running the script in elevated?
In the same console where you're running the script, enter
whoami.exe /groups

Open in new window

Towards the end, there should be an entry under "Group Name" named "Mandatory Label\High Mandatory Level". If it's "Mandatory Label\Medium Mandatory Level" instead, the console isn't running elevated.

Author

Commented:
Brilliant work.  I ran it under elevated access and those files were deleted.  Using that information, I created a task under task scheduler and had it run with highest privileges.  It ran successfully that way as well.  I guess I assumed using Powershell ISE on server 2012 that it would run with the higher privileges.

Thank you for your help and explanation!

Author

Commented:
If there's a Mount Rushmore for EE, oBdA has to be on there.