Working with file modification time and date using PowerShell

How to find files modified before certain date from folder in PowerShell

find files modified in last 24 hours from folder using PowerShell, find files modified in last 7 Days from folder using PowerShell, find files modified in last one month from folder in PowerShell, find files modified in current month from folder in PowerShell, find files modified in last one year from folder in PowerShell, find files modified in current year from folder in PowerShell, find files modified before certain date from folder in PowerShell, find Files Modified between dates from folder.
Here we will discuss below things:
– How to find files modified in last 24 hours from a folder using PowerShell?
– How to find files modified in last 7 Days from a folder using PowerShell?
– How to find files modified in last one month from a folder in PowerShell?
– How to find files modified in current month from a folder in PowerShell?
– How to find files modified in last one year from a folder in PowerShell?
– How to find files modified in the current year from a folder in PowerShell?
– How to find files modified before certain date from a folder in PowerShell?
– How to find Files Modified between dates from a folder using PowerShell?

All the PowerShell scripts you can run and debug using Microsoft PowerShell ISE.

SharePoint deveopment training course

Find files modified in last 24 hours from a folder using PowerShell:

We can retrieve all files modified in last 24 hours from a folder using PowerShell. Below is the PowerShell script.

Here we have taken a $hours_to_check variable and adding -24 hours to it. Then we are retrieving all files by using Get-Item PowerShell cmdlet, in the where condition we are filtering records which have modified tine greater the above calculated hours.

$hours_to_check=$(Get-Date).AddHours(-24)

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -gt $hours_to_check } | Foreach {

“File Name: ” + $_.Name

}

The output will appear like below:

find files modified in last 24 hours from folder using PowerShell
find files modified in last 24 hours from folder using PowerShell

Find files modified in last 7 days from a folder in PowerShell:

Below is the PowerShell script by using which we will be able to retrieve files modified in last 7 days.

Here we have taken a variable as $days_to_check and adding -7 days to it. And then by using PowerShell cmdlet Get-Item we are retrieving all items from the folder and then in the where condition we are filtering files modified in last 7 days comparing with the LastWriteTime of the files.

$days_to_check=$(Get-Date).AddDays(-7)

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -gt $days_to_check } | Foreach {

“File Name: ” + $_.Name

}

The output will appear like below:

find files modified in last 7 Days from folder using PowerShell
find files modified in last 7 Days from folder using PowerShell

Find files modified in last one month from a folder in PowerShell:

We can also easily find files modified in last one month using PowerShell. Here I have taken a variable $month_to_check and adding -1 months to the variable. And then by using Get-Item cmdlets, we are retrieving all the files from the folder. Then in the where condition we are filtering the files based on LastWriteTime. Below is the full PowerShell script.

$month_to_check=$(Get-Date).AddMonths(-1)

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -gt $month_to_check } | Foreach {

“File Name: ” + $_.Name

}

When you run the above script, it will display the output like below:

How to find files modified in last one month from folder in PowerShell
How to find files modified in last one month from folder in PowerShell

Find files modified in current month from a folder in PowerShell:

Here we will see how we can find files modified in the current month using PowerShell. We have taken a variable $month which will contain the current month.

Then by using Get-Item cmdlet, we are retrieving all the files and in the Where filter, we are filtering records whose lastWriteTime equals to today’s month.

$month=(Get-Date).Month

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime.Month -eq $month } | Foreach {

“File Name: ” + $_.Name

}

You can see the output like below:

How to find files modified in current month from folder in PowerShell
How to find files modified in current month from folder in PowerShell

Find files modified in last one year from a folder in PowerShell:

We can also retrieve all the files modified in the last one year. Here I have taken a variable $year_to_check and into that, I have added -1 year. Then by using Get-Item cmdlet, I am retrieving all files and then we are filtering in where condition by using LastWriteTime greater than one last one year.

$year_to_check=$(Get-Date).AddYears(-1)

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -gt $year_to_check } | Foreach {

“File Name: ” + $_.Name

}

Once you run the above script, the result will appear like below:

 

How to find files modified in last one year from folder in PowerShell
How to find files modified in last one year from folder in PowerShell

Find files modified in the current year from a folder in PowerShell:

We can easily find files modified in the current year using PowerShell. Here we have to get the current year and assigning to a variable as a $year and then by using Get-Item cmdlet we are retrieving all the files from the folder. Then by in the Where condition, we are filtering files whose LastWriteTime is the current year.

Below is the PowerShell script to retrieve all the files modified in the current year.

$year=(Get-Date).Year

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime.Year -eq $year } | Foreach {

“File Name: ” + $_.Name

}

The output will appear like below:

How to find files modified in current year from folder in PowerShell
How to find files modified in current year from folder in PowerShell

Find files modified before certain date from a folder in PowerShell:

Here we will see how we can find files modified before a certain date using PowerShell. I have a few files in one folder, my requirement is to retrieve files modified before a certain date. Here I have taken the particular date in a variable $date_to_compare and then we are retrieving all the files from the folder using the Get-Item cmdlet. And then in the where condition we are filtering the files whose LastWriteTime is less than the date provided in the variable.

Below is the full PowerShell script to find files modified before a certain date.

#Date in “mm/dd/yyyy”

$date_to_compare=”01/30/2018″

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -lt $date_to_compare } | Foreach {

“File Name: ” + $_.Name

}

Once you run the script the output will appear like below:

How to find files modified before certain date from folder in PowerShell
How to find files modified before certain date from folder in PowerShell

Also, you can read:
Upload large files to SharePoint online document library using PowerShell csom

Steps to add items from CSV file to SharePoint Online List using PowerShell in CSOM

Export User Permissions to HTML using PowerShell in SharePoint Online

Find Files Modified between dates from a folder using PowerShell:

Now finally we will discuss how we can retrieve files modified between two dates using PowerShell. Here I have few files inside a folder and by using PowerShell we will retrieve files modified between two specified dates.

Here I have taken those two dates in two variables. Then by using Get-Item cmdlets, I am retrieving all the files from the folder. And in the where condition we are filtering files modified between the specified dates.

Below is the PowerShell script to find Files Modified between dates from folder.

#Date in “mm/dd/yyyy”

$start_date=”01/29/2018″

$end_date=”02/03/2018″

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where {

$_.LastWriteTime -gt $start_date -AND $_.LastWriteTime -lt $end_date} | Foreach {

“File Name: ” + $_.Name

}

Once you run the above script, the output will appear like below:

How to find Files Modified between dates from folder using PowerShell
How to find Files Modified between dates from folder using PowerShell

Hope this PowerShell tutorial will help you.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →