How to get file modification time and date using PowerShell

In this PowerShell SharePoint tutorial, we will discuss how to work with file modifications time and date using PowerShell.

Here we will discuss the 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.

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

Read How to upload files to SharePoint using PowerShell

See also  Create a SharePoint Online Communication Site using Power Automate [Download Complete Package]

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 a folder in PowerShell
Find files modified in last 7 days from a folder in PowerShell

Read PowerShell SharePoint list operations

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:

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

Read PowerShell SharePoint site collection example

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:

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

Read How to Change SharePoint Site Logo + PowerShell

See also  Run JavaScript after page loaded completely using jQuery

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:

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

Read Get sharepoint site template 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:

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

Read How to remove SharePoint Online Modern Page Title using 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.

See also  How to find product number of hp laptop using command prompt

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:

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

Read How to activate SharePoint Publishing Feature Programmatically using CSOM and PowerShell

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 a 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 a 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:

powershell find files modified between two dates 1
Find Files Modified between dates from a folder using PowerShell

You may also like the following PowerShell tutorials:

Tags:

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.

>