PowerShell find files modified in last N days

in this PowerShell tutorial, we will discuss how to get files modified in last N days using PowerShell. In this particular example, we will discuss how to find files modified in the last 7 days using a PowerShell script.

Here I have a folder, in my local drive which has few files. By using the PowerShell script, we will find out files modified in the last 7 days.

PowerShell find files modified in last N days

Below is the PowerShell script:

Here we are first calculating, for how many days we want data. We are checking here for the last 7 days.

In PowerShell, by using Get-Item we can retrieve all items from a folder and then we are writing the where condition and filtering record for the last 7 days.

Then in the Foreach we are getting the file names. In PowerShell $_ represents the current object.

$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
}
PowerShell find files modified in last 7 days
PowerShell find files modified in last 7 days

Download in CSV file format:

If you want to download the file in csv format, then you can modify like below:

$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 >> C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\filenames.csv
}

If you run the PowerShell script, it will download the result in csv format.

Read some PowerShell tutorials:

Hope this article will be helpful to find files modified in the last N days in PowerShell.

>