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 and this guide explains the script and how to use it.
Overview of the Function
SysAdmins requests all sorts of requests. One of the ways 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 Function
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 Function
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. |
How to Download and Install the Function
Before you can use this custom function, you need to download and install it. Follow the steps below to complete this task.
- 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.
- 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.
- After copying the Get-ItemsBetweenDates folder to “\Program Files\WindowsPowerShell\Modules,” proceed to step 4 below.
- Open PowerShell as administrator and run the following commands:
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 Use the File and Directory Parameters
As you would have noted in the Parameters section of this guide, the Get-ItemsBetweenDates has the File and the Directory parameters.
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
Find Files and Folders Last Modified from Specific Date
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.
Find Only Folders Modified Between Two Dates
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
Frequently Asked Questions
In PowerShell, the LastWriteTime of an item is the last date/time that the item (file or folder) was modified.
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
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
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.
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
Conclusion
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.
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 by responding to the “Was this page helpful?” question below.
Finally, for more custom PowerShell functions, visit our PowerShell Script Repository page.