I'm working on keeping a archive directory of folders. Each folder contains multiple files representing a backup of an application instance for the day and the folder is created (and named) based on the day the backup was created. I want to keep X folders available at a given time and delete the older folders.
I attempted to implement a solution based on this previously answered question.
My new code is as follows:
gci E:\sql_backups\Everything\Archive -Recurse|where {-not $_PsIsContainer}|sort CreationTime -desc| select -Skip X| Remove-Item -Force
There are two issues I'm running into. The first is that when the script is run it will still prompt for the recursion UI prompt for recursion.
The second issue is that it is now removing all folders from the directory, regardless of the value of X.
Since you are strictly returning files, you will have to do a little more work. As I commented, you are missing a period in
$_.PsIsContainer. This should achieve your desired result.Also, in powershell version 3.0 and up there exist
-Fileand-Directoryparameters so you can replace your where clause with-File