I have an SSIS package where one of the tasks that I'm running uses powershell to sort files by Modified Date descending and then deletes all the files but the file that had the most recent modification. When the powershell script runs, it ends up deleting all but the second file from the top of the list; so the second most recently modified file. I've tried troubleshooting this issue in Powershell ISE to get a better idea of what's going on, but I'm running across strange behavior. I recently changed the script to sort the files by the LastWriteTime instead, but I get the same result.
I'm new to scripting with PowerShell, so forgive my ignorance. Here's what I've tried.
The first line below is to view the files that I'm working with and to see what the LastWriteTime is.
The second line is part of the actual script; I add more code to actually remove the files.
Does anyone have any ideas as to why powershell sorts the files in the second result not in descending order when that's what I've specified?
PS D:\Vivid\2008R2_Solution\Vivid-SCP\vividreports> get-childitem -filter "*.xls" | sort LastWriteTime -DescendingDirectory: D:\Vivid\2008R2_Solution\Vivid-SCP\vividreports
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 06/01/2013 4:00 AM 115200 managerReport_06_2013.xls
-a--- 04/04/2013 12:00 AM 115200 managerReport_04_2013.xls
-a--- 02/18/2013 11:00 PM 111616 managerReport_02_2013.xls
-a--- 01/28/2013 11:00 PM 81920 managerReport_01_2013.xls
PS D:\Vivid\2008R2_Solution\Vivid-SCP\vividreports> get-childitem -name -filter "*.xls" | sort LastWriteTime -Descending
managerReport_04_2013.xls
managerReport_06_2013.xls
managerReport_01_2013.xls
managerReport_02_2013.xls