PowerShell Find Files Modified Between Dates

[ad_1]

Do you want a PowerShell script to find files or folders modified between two dates? Instead of creating a simple script, I created a function (Get-ItemsBetweenDates) that does this!

In the first section of this guide, you will get an overview of the Get-ItemsBetweenDates function. Then, in the second and third sections, you will learn the syntaxes and parameters of the function.

Meanwhile, in section four, I’ll give some examples of how to use Get-ItemsBetweenDates. I also have an FAQ section where I answer common questions about finding modified files between dates with PowerShell.

Overview Of Get-ItemsBetweenDates (PowerShell Function To Find Files Modified Between Dates)

Overview Of Get-ItemsBetweenDates (PowerShell Function To Find Files Modified Between Dates)

SysAdmins request all sorts of requests. One of the ways a SysAdmins can improve her efficiency is by automating tasks with PowerShell.

One common request is to find files or folders that are modified on a specific date. In addition to finding files modified on a specified date, you may need to find files with the last modified dates between two dates.

This is where Get-ItemsBetweenDates comes in.

Instead of creating a PowerShell script to find files modified between dates, you can use the Get-ItemsBetweenDates. All you need is to specify the path you want to find the files.

Then, specify the day range you want to consider, and bingo, you have your files and/or folders!

Syntaxes Of The Get-ItemsBetweenDates

How To Use PowerShell To Find Files Or Folders Modified Between Two Dates - Syntaxes Of The Get-ItemsBetweenDates

The Get-ItemsBetweenDates command has three syntaxes. Here they are…

Get-ItemsBetweenDates [-Path] <String[]> [-NumOfDaysFrom] <Int32> [-NumOfDaysTo] <Int32> [<CommonParameters>]
    
Get-ItemsBetweenDates [-Path] <String[]> [-NumOfDaysFrom] <Int32> [-NumOfDaysTo] <Int32> [[-Directory]] [<CommonParameters>]
    
Get-ItemsBetweenDates [-Path] <String[]> [-NumOfDaysFrom] <Int32> [-NumOfDaysTo] <Int32> [[-File]] [<CommonParameters>]

If you want to return files and folders between dates, run the Get-ItemsBetweenDates PowerShell function with the first syntax.

However, if you want to return just files, run the command with the second syntax. Similarly, to return just folders, use the third syntax.

Parameters Of The Get-ItemsBetweenDates

The Get-ItemsBetweenDates has five parameters. In the table below, I have listed all the parameters and what they do.

Parameters of Get-ItemsBetweenDates Comments/Notes
Path Specifies the path to the folder you want Get-ItemsBetweenDates to return items from. This parameter accepts wildcards. See examples for details.
NumOfDaysFrom This will be the earliest number of days you want to return files with modified dates from today. Specify this in numbers, NOT dates.
NumOfDaysTo This will be the latest number of days from today that you want to return files with modified dates. Like the NumOfDaysFrom parameter, specify a number. Note that NumOfDaysTo MUST be smaller than the NumOfDaysFrom parameter.
File This is a Switch parameter type. By default, Get-ItemBetweenDates returns both files and folders. However, if a user wants this function to return only files, the user will include the File parameter.
Directory Like the File parameter, this is also a Switch parameter type. If a user wants this function to return only folders, the user will include Directory. Note that you cannot specify File and Directory parameters in the same command. You can either specify File or Directory in one command.

Use PowerShell To Find Files Or Folders Modified Between Two Dates (Get-ItemsBetweenDates): Examples

Use PowerShell To Find Files Or Folders Modified Between Two Dates (Get-ItemsBetweenDates) Examples

Now that you know the syntax and parameters of the Get-ItemsBetweenDates PowerShell function, it is time to learn how to download and install the function.

In this section, I also discussed some examples of using the Get-ItemsBetweenDates PowerShell function.

How To Download And Install The Get-ItemsBetweenDates PowerShell Function

Before you can use this custom function, you need to download and install it. Follow the steps below to complete this task.

  1. To download the Get-ItemsBetweenDates zip file, click download Get-ItemsBetweenDates. When you click the link, your browser will down a zip file called Get-ItemsBetweenDates.zip.
  2. Next, unzip Get-ItemsBetweenDates.zip and save the unzipped Get-ItemsBetweenDates folder in the path below:
C:\Program Files\WindowsPowerShell\Modules

To make sure that you get the above path right, enter the command below in PowerShell and press enter.

$env:ProgramFiles + "\WindowsPowerShell\Modules"

If this path does not exist, you need to create it. To create the full path, you can open PowerShell as administrator and run the following commands:

$ModulesFolder = $env:ProgramFiles + "\WindowsPowerShell\Modules"
New-Item -Path $ModulesFolder -ItemType Directory -Force | Out-Null

Once you have created the above path (if it does not exist), copy the unzipped folder, Get-ItemsBetweenDates, to the last folder in the path…

When you attempt to copy the folder to “\Program Files\WindowsPowerShell\Modules,” you will receive a prompt to provide administrator permission to copy the folder. To provide the administrator permission, click Continue.

Use PowerShell To Find Files Or Folders Modified Between Two Dates (Get-ItemsBetweenDates): Examples
  1. After copying the Get-ItemsBetweenDates folder to “\Program Files\WindowsPowerShell\Modules,” proceed to step 4 below.
  1. Open PowerShell as administrator and run the following commands:

If PowerShell was open before you copied Get-ItemsBetweenDates to “\Program Files\WindowsPowerShell\Modules,” you MUST close and re-open PowerShell before you run the command below.

Import-Module Get-ItemsBetweenDates -Force

Since you downloaded the file from the internet, PowerShell will display a warning. To proceed, enter R at the prompt and press the enter key on your keyboard.

Now that you have downloaded and installed the Get-ItemsBetweenDates PowerShell function, you can use it to find files (or folders) modified between two dates.

In the following sub-sections, I have shared some examples of how to use Get-ItemsBetweenDates.

You can also read these examples within PowerShell by running the command below.

Get-Help Get-ItemsBetweenDates -Examples

How To Find Files And Folders Last Modified Between Two Dates With The Get-ItemsBetweenDates PowerShell Function

As you would have noted in the Parameters section of this guide, the Get-ItemsBetweenDates has the File and the Directory parameter.

These parameters are optional, meaning that you do not have to specify them.

Therefore, if you do not specify the File or the Directory parameter, Get-ItemsBetweenDates returns both files and folders (directories) in a specified path.

So, to return all files and folders modified between 10 days ago and 40 days ago in the path “D:\PowerShell Scripts” on my computer, I’ll run the command below:

Get-ItemsBetweenDates -Path "D:\PowerShell Scripts\*" -NumOfDaysFrom 40 -NumOfDaysTo 10
How To Find Files And Folders Last Modified Between Two Dates With The Get-ItemsBetweenDates PowerShell Function

How To Find Files Last Modified Between Two Dates With The Get-ItemsBetweenDates PowerShell Function

In the last example, I used the Get-ItemsBetweenDates PowerShell function to return files and folders modified between two dates.

What if I want to return just Files? I will include the File parameter as shown in the command below:

Get-ItemsBetweenDates -Path "D:\PowerShell Scripts\*" -NumOfDaysFrom 40 -NumOfDaysTo 10 -File

As you can see from the result of the command, it returned just a file and left out the folders.

How To Find Folders Last Modified Between Two Dates With The Get-ItemsBetweenDates PowerShell Function

Unlike in the last example, where I returned just files, I may decide to return just folders alone. The command below (that includes the Directory parameter) does the trick:

Get-ItemsBetweenDates -Path "D:\PowerShell Scripts\*" -NumOfDaysFrom 40 -NumOfDaysTo 10 -Directory

The command returns just folders!

How To Find Folders Last Modified Between Two Dates With The Get-ItemsBetweenDates PowerShell Function

Frequently Asked Questions About Using PowerShell To Find Files Or Folders Modified Between Two Dates

Frequently Asked Questions About Using PowerShell To Find Files Or Folders Modified Between Two Dates
1. What Is LastWriteTime In PowerShell?

In PowerShell, the LastWriteTime of an item is the last date/time that the item (file or folder) was modified.

2. How Do I Get The Modified Date Of A File In PowerShell?

When you run the Get-ChildItem command, it returns a property known as LastWriteTime. This property holds the information about the last time a file was modified.

I will run the command below to return the name and last modified date of all files in the path “D:\PowerShell Scripts”.

Get-ChildItem -Path “D:\PowerShell Scripts\*” -File | Select-Object Name, LastWriteTime

3. How Do I Get LastWriteTime In PowerShell?

To get the LastWriteTime of a file or folder, run the Get-ChildItem. Then, pipe the output to the Select-Object cmdlet and return LastWriteTime.

The command below returns the LastWriteTime of all files and folders in the path, “D:\PowerShell Scripts”:

Get-ChildItem -Path “D:\PowerShell Scripts\*” | Select-Object LastWriteTime

4. How Do I Filter In PowerShell?

PowerShell has numerous cmdlets that you can use to filter objects. Some of these cmdlets are Where-Object, Select-Object, and Select-String.

Others are ForEach-Object and Out-GridView. In addition to these cmdlets, there are come cmdlets that include filtering parameters.

For example, the Get-ChildItem cmdlet has the File and Directory parameters. You can use these parameters to return only files or only folders, respectively.

Regarding the “filter” cmdlets I listed earlier, you can use them individually or combine them to filter objects. For example, you can pipe (|) the output of the Get-ChildItem cmdlet to Where-Object.

Then, use Where-Object to filter the output you need to return. Finally, you may decide to pipe the output of Where-Object to Select-Object.

With the Select-Object cmdlet, you can decide which properties of Get-ChildItem to return in the final results.

5. What Does Recurse Mean In PowerShell?

Cmdlets that include the Recurse parameter use this parameter to tell PowerShell to process data in a specified directory and all its subdirectories.

To list all cmdlets that support the Recurse parameter, run the following PowerShell command:

Get-Command -ParameterName Recurse

My Final Thoughts About Using PowerShell To Find Files Or Folders Modified Between Two Dates

You can use the Get-Date, Get-ChildItem, Where-Object, and Select-Object PowerShell cmdlets to find files modified between dates. However, the command to achieve this is fairly complicated.

Also, if you were to use these commands to find files modified between dates, every time you run the command, you have to manually use Get-Date to get the date version of the days you want to return files or folders for.

Instead of doing all that, I created the Get-ItemsBetweenDates PowerShell function. With this function, you specify 3 parameters, and PowerShell returns your desired files and folders modified between the specified days.

By creating this function, I hope I was able to solve a major problem for you. If I did, kindly spare about 2 minutes to share your experience at Itechguides Community Forum.

Also, if you have questions about the Get-ItemsBetweenDates function or if the function does not meet a specific need for you, you can also reply to this article’s topic at Itechguides Community Forum. Although I am busy, I find time to respond to all PowerShell questions posted in Itechguides Community Forum.

Finally, for more custom PowerShell functions, visit our PowerShell Script Repository page.

References And Further Reading

I used the following PowerShell cmdlets to build the Get-ItemsBetweenDates PowerShell function. To learn more about each cmdlet, click the link.

  1. Get-Date (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Docs
  2. Get-ChildItem (Microsoft.PowerShell.Management) – PowerShell | Microsoft Docs
  3. Where-Object (Microsoft.PowerShell.Core) – PowerShell | Microsoft Docs
  4. Select-Object (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Docs

Leave a Reply

Your email address will not be published. Required fields are marked *