How to use PowerShell to find the oldest file on your system

I recently wrote an article in which I talked about how to perform various date manipulations in PowerShell. Within that article, I wrote a simple PowerShell script that compiles a list of the files that are older than a specific date. The idea is that you may be able to reduce storage costs by locating and then archiving aging files. After writing that article, I began to wonder if there might be a way to use PowerShell to locate the oldest file in a folder, or even perhaps the oldest file on an entire volume. As it turns out, it is fairly easy to build on the concepts that I had previously discussed, and create such a script.

For the sake of review, here is what my original date comparison script looked like:


$A = (Get-Date -Year 2017 -Month 01 -Day 02)
$A
$X = Get-ChildItem Readme.txt
$X.LastWriteTime
$X.LastWriteTime -GT $A


This particular script was super-simple. I stored the date against which I wanted to make a comparison (in this case, January 2, 2017) in a variable called $A. I then created a second variable named $X, which I mapped to a file (in this case Readme.txt). I then compared the file date ($X.LastWriteTime) against the comparison date ($A). The script returned a value of True if the file was more recent than the date stored in $A. Simple enough.

Oldest file on a volume

As previously noted, however, this same concept can be extended in an effort to find the oldest file on a volume. Here is what the script looks like:


$FileDate = (Get-Date -Format g)
$path = “C:\”
Get-ChildItem -Path $path -Recurse | ForEach-Object {
if ($_.LastWriteTime -lt $FileDate -and -not $_.PSIsContainer) {
$FileDate = $_.LastWriteTime
$OldFile = $_.FullName
}
}
Write-Host ‘The oldest file on the system is: ‘ $OldFile
$FileDate


oldest file

As you can see, this script is slightly more advanced than the previous script, but it still uses the same basic principles as before.

So the first thing that this script does is to create a variable called $FileDate. The $FileDate variable is set to today’s date and to the current time. The reason why I am doing this is because it is only possible to find the oldest file on a system by making a series of comparisons, and we need an initial value to compare against. It’s a safe bet that the oldest file on a system is going to be older than the current date and time, so the current date and time makes for a good starting point.

The next line in the script sets the search path. Right now I have the search path set to C:\, but you can use a different path if you want. It is worth noting that if you plan to search a network path, then you probably won’t be able to use a network drive letter in the $Path variable. I was only able to make the script work against a network drive by assigning the $Path variable to a UNC path (such as \\192.168.0.1\h$).

The third line of the script retrieves the file that is being compared. In the simple comparison script that I presented at the beginning of this article, I used the Get-ChildItem cmdlet in conjunction with a specific filename (Readme.txt). In this case, however, I am using the -Path parameter to point the cmdlet to a path rather than having it examine a single file. You will also notice that I am using the -Recurse parameter. This parameter tells the Get-ChildItem to check any existing subfolders beneath the specified path. If you only want to check files within the specified path, but not in any subfolders, then just omit the -Recurse parameter.

This same line of code also sets up a ForEach-Object loop. This allows us to examine each file individually.

There are three lines of code within the ForEach-Object loop. The first line performs the actual date comparison by comparing the file’s date ($_.LastWriteTime) against the starting date ($FileDate). Incidentally, the comparison is being made by using the -LT operator. If you were to change this to -GT (and manually set the $FileDate variable in the first line to a past date), the script will locate the newest file on the system.

This line of code also checks to make sure that we are examining a file, not a container (-and -not $_.PSIsContainer).

Riding the loop

If the file does end up being older than the current date and time, then the script executes another loop of code. This loop does two things. First, it modifies the $FileDate variable to store the file’s date and time stamp rather than the current date and time. That way, subsequent comparisons will be made against the file’s date rather than the current date. Each time that the script finds a file that is older than whatever happens to be stored in the $FileDate variable, it overwrites the $FileDate variable with the file’s date.

The other thing that the loop does is to assign the file’s full name to a variable named $OldFile. In case you are wondering $_.FullName includes both the filename and its path. If for whatever reason you only wanted to know the filename, but not the path, you could use $_.Name in place of $_.FullName.

Upon completion of the loop, the $OldFile variable will contain the path and filename of the oldest file on the system. The $FileDate variable will store the file’s date. Now the only thing left to do is to output the information. For that, I am simply using the Write-Host command and the variable names that I want to output. You can see the script’s output in the figure below.oldest file

Take it from here

In the real world, you may not have a need to locate the oldest file on an entire volume. However, this script can be easily modified to locate the oldest file within a specific folder. This might be useful for example if you want to purge old log files or find the oldest version of a PowerShell script.

Featured image: Shutterstock

About The Author

2 thoughts on “How to use PowerShell to find the oldest file on your system”

  1. Thanks for the script!
    I had to change the date initialization “Get-Date -Format g” to “Get-Date”

    The format would otherwise not match my system date format
    “Get-Date -Format g” results in something like “MM/DD/YYYY time”
    while my system uses “DD.MM.YYYY time” and PowerShell failed to compare the two format

    1. Thanks for the feedback. I hadn’t even thought about that a comparison might not work if a system was configured to use an alternate date format.

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top