≡ Menu

PSTip# Get oldest folder/file in a folder using Powershell

In this post let see how to find out the older item(be it folder or file) in a given folder. This can be achieved by reading all files/folders in the given path using Get-ChildItem cmdlet and then sorting by CreationTime Property of each returned output. By default Sort-Object does sorting by ascending order. After sorting, the output is further filtered out using select statement and -First parameter to get the first value in the sorted list which is the oldest item in the folder.

Below is the code.

File Name: Get-OldestItemInFolder.ps1


$Item = Get-ChildItem -Path $FolderName | Sort CreationTime | select -First 1            
Write-Host "Oldest file/folder in $FolderName is $($Item.FullName)"            
Write-host "Creation Time is $($item.creationtime)"


This script takes optional -FolderName parameter if you want to give a specific folder. Otherwise it will run against current directory from where the script is triggered.

{ 0 comments… add one }

Leave a Comment