Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell Script to archive files over 1 day

I am looking for a script that can ignore the timing and utilise just the date to move files after 1 day, so yesterday, to an archive folder.

My knowledge of powershell is not great so any advice on how i can do this would be great.

Everyday i run a script that generates a .txt report which has a filename …..2022 01 02 (The filename ends with the date) so would like to add some extra lines that archives the .txts that were created yesterday to an archive folder.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The [datetime] type has a Date property that gives you the date at midnight, thereby allowing you to compare dates without taking the time component into account:

# Construct datetime value describing yesterday at midnight
$yesterday = (Get-Date).AddDays(-1).Date

# Filter files based on date of the `CreationTime` property
$filesCreatedYesterday = Get-ChildItem -Path .\path\to\folder -File |Where-Object { $_.CreationTime.Date -eq $yesterday }

$filesCreatedYesterday will now contain the files created yesterday

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading