Q about deleting files and folders older than a certain date

4 views
Skip to first unread message

Mike Leone

unread,
Nov 12, 2025, 3:40:11 PMNov 12
to NTPowershell Mailing List
Can somebody verify something for me, please. We have a VM with literally millions of files on it. Finally I've been told that I can delete anything older than 500 days ... which is still a couple million files to get rid of. So here's the thing ...

This is obviously a structure with a lot of sub-folders.

i.e.

E:\ps\0_DC_loan>dir /s /b
E:\ps\0_DC_loan\Cecily
E:\ps\0_DC_loan\Cecily\59_Comb_Fin.xlsx
E:\ps\0_DC_loan\Cecily\New DC Files
E:\ps\0_DC_loan\Cecily\Old DC Files
E:\ps\0_DC_loan\Cecily\Union_Comb_Fin.xlsx
E:\ps\0_DC_loan\Cecily\New DC Files\216638_141225_150202_F0132421 ck dt 1.2.15-new.xlsx
E:\ps\0_DC_loan\Cecily\New DC Files\216638_141225_150202_F0132421 ck dt 1.2.15.txt
E:\ps\0_DC_loan\Cecily\New DC Files\216638_141225_150202_F0132433_U ck dt 1.2.15-new.xlsx

and so on ...

So the question is ... some of those sub-folders may have files that shouldn't *yet* be deleted. 

Fine.

If I do 

$AllFiles = @(Get-ChildItem $ProcessingFolder -Recurse -ErrorAction SilentlyContinue -Force)

Then loop through $AllFiles, checking the $_.LastWriteTime against my target date, I can do
Remove-Item

BUT ... what if that entry is a directory? I may not want to remove the whole directory, because there may be files in there I want. Or will there? Will the LastWriteTime of the directory be updated, if a newer file or folder is written into it? ("Yes" is what I think)

Basically, am I safe to remove a directory that has a LastWriteTime in the period I want, because any file/folder in there (or *under* there) should have a LastWriteTime in the period I want?

In the example above, the folder "New DC Files" would have a LastWriteTime in the period I want to get rid of, as long as EVERY file or folder *within* "New DC Files" is within my time period. If any file/folder under it is newer, then "New DC Files" folder itself would not be deleted (along with the newer file, of course).

I'm trying to make sure I don't leave empty directories behind, as well as not inadvertently deleting any file/directory newer than the time period I am interested in.

Since I never doubt that I'm missing some concept ... what am I missing here?
--

Mike. Leone, <mailto:tur...@mike-leone.com>

PGP Fingerprint: 0AA8 DC47 CB63 AE3F C739 6BF9 9AB4 1EF6 5AA5 BCDF
Photo Gallery: <http://www.flickr.com/photos/mikeleonephotos>

Belanger, Xavier B

unread,
Nov 12, 2025, 3:47:30 PMNov 12
to NTPowershell Mailing List
Hi,

According to the documentation you can exclude directories using the appropriate attribute:

Get-ChildItem -Attributes !Directory


If that is not available for the Powershell version that you're using you can also use this:

Get-ChildItem -Recurse | Where-Object {$_.psIsContainer -eq $False}

Sincerely,

-- 
Xavier Belanger

IT Security Architect | CISSP | Office of Information Security

University of North Carolina Wilmington

 


From: ntpowe...@googlegroups.com <ntpowe...@googlegroups.com> on behalf of Mike Leone <tur...@mike-leone.com>
Sent: Wednesday, November 12, 2025 15:41
To: NTPowershell Mailing List <ntpowe...@googlegroups.com>
Subject: [ntpowershell] Q about deleting files and folders older than a certain date
 

[This email originated from outside of UNCW]

--
You received this message because you are subscribed to the Google Groups "ntpowershell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntpowershell...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ntpowershell/CAHBr%2B%2BhiH0tfFX1h%2Bu76_xVQ3C2%2BVMPzjbHwS7y7zfa2COFqFQ%40mail.gmail.com.

Michael B. Smith

unread,
Nov 12, 2025, 3:57:51 PMNov 12
to ntpowe...@googlegroups.com

On ps 5.1, use -DIR to GCI. Of course, the psIsContainer also works, but is slower.

Wright, John M

unread,
Nov 13, 2025, 8:10:43 AMNov 13
to ntpowe...@googlegroups.com

You should be able to use the -File parameter to only capture files.  The only drawback is that you may leave empty directories.

 

--

John Wright

IT Support Specialist

1800 Old Bluegrass Avenue, Louisville, KY 40215

502.708.9953

Please submit IT requests to Hazelwoo...@bluegrass.org

24 Hour Helpline 1.800.928.8000

  

CONFIDENTIALITY NOTICE: This message contains confidential information and is intended only for the individual(s) addressed in the message. If you are not the named addressee, you should not disseminate, distribute, or copy this e-mail. If you are not the intended recipient, you are notified that disclosing, distributing, or copying this e-mail is strictly prohibited.

 


Sent: Wednesday, November 12, 2025 3:41 PM
To: NTPowershell Mailing List <ntpowe...@googlegroups.com>

Subject: [ntpowershell] Q about deleting files and folders older than a certain date

 

EXTERNAL EMAIL - This email was sent by a person from outside your organization. Exercise caution when clicking links, opening attachments or taking further action, before validating its authenticity.

Secured by Check Point

Markus Klocker

unread,
Nov 13, 2025, 8:22:17 AMNov 13
to ntpowe...@googlegroups.com
what about
 gci . *.* -rec | where { ! $_.PSIsContainer }

should only get files this way.

hth
    Markus

Mike Leone

unread,
Nov 13, 2025, 10:57:45 AMNov 13
to ntpowe...@googlegroups.com
On Thu, Nov 13, 2025 at 8:10 AM Wright, John M <John....@newvista.org> wrote:

You should be able to use the -File parameter to only capture files.  The only drawback is that you may leave empty directories.


Thanks. Yes, that was my biggest worry, leaving leftover empty directories.

UPDATE: I spoke to the application owner, I had the filepath to be cleaned up wrong. Apparently there is only 1 folder, and all files are in it - there are no sub-folders in this folder. It's effectively just a big old dump pocket for scanned images, no further organization (in terms of sub-folders, etc).

Which then makes my life immeasurably easier, as I just need to cycle through 1 folder, checking just each file date, and deleting as appropriate.

But it's good to know, for the future!
 
Reply all
Reply to author
Forward
0 new messages