It is easy to get files from a folder using PowerShell based on some conditions. In this PowerShell tutorial, I will show you multiple methods to get the first 10 files in a folder using PowerShell, ordered by their last modified date.
Method 1: Using Get-ChildItem and Sort-Object
To get the first 10 files in a folder using PowerShell, you need to get the files and then sort them by their last modified date.
The simplest ways to retrieve files and sort them by their last modified date is by using the Get-ChildItem cmdlet in combination with Sort-Object and Select-Object.
Here is the complete PowerShell script.
# Define the path to the folder
$folderPath = "C:\MyFolder"
# Get the first 10 files ordered by last modified date in descending order
Get-ChildItem -Path $folderPath |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -First 10In this example:
Get-ChildItemretrieves all files and directories in the specified folder.Sort-Objectsorts the items by theLastWriteTimeproperty in descending order.Select-Objectselects the first 10 items from the sorted list.
You can see I executed the PowerShell script using VS code, and it sent me the first 10 files from the folder.

Method 2: Using Get-ChildItem with Where-Object
If you want to filter files before getting the first 10 files from a folder using PowerShell, then you can use the Get-ChildItem with Where-Object cmdlets.
Here is the complete PowerShell script.
# Define the path to the folder
$folderPath = "C:\MyFolder"
# Get the first 10 files modified in the last 30 days, ordered by last modified date
Get-ChildItem -Path $folderPath |
Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-30) } |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -First 10In this example:
Where-Objectfilters the files to include only those modified in the last 30 days.- The rest of the pipeline remains the same as in Method 1.
You can see the output like in the below screenshot after I executed the script using VS code.

Method 3: Recursive Search with -Recurse
If you want to get the files from subfolders also, then you can use the -Recurse parameter with Get-ChildItem cmdlet.
Here is the complete PowerShell script.
# Define the path to the folder
$folderPath = "C:\MyFolder"
# Get the first 10 files from the folder and its subfolders, ordered by last modified date
Get-ChildItem -Path $folderPath -Recurse |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -First 10In this example:
- The
-Recurseparameter makesGet-ChildItemsearch within all subfolders. - The rest of the pipeline remains the same as in Method 1.
Method 4: Using ForEach-Object
In PowerShell, you can also use the PowerShell ForEach-Object cmdlet to get the first 10 files from a folder.
Here is complete example.
# Define the path to the folder
$folderPath = "C:\MyFolder"
# Initialize a counter
$counter = 0
# Process each file and break after 10 files
Get-ChildItem -Path $folderPath |
Sort-Object -Property LastWriteTime -Descending |
ForEach-Object {
if ($counter -lt 10) {
$_
$counter++
} else {
break
}
}In this example:
ForEach-Objectprocesses each file individually.- A counter is used to keep track of the number of files processed.
- The loop breaks after 10 files have been processed.
I executed the complete PowerShell script, and you can see the output in the screenshot below:

Conclusion
I hope you can use the above methods to get the first 10 files from a folder using PowerShell. The PowerShell cmdlets are like: Get-ChildItem, Sort-Object, and Select-Object, etc.
You may like the following PowerShell tutorials:
- How to Get Unique Lines from a File Using PowerShell?
- How to search for files recursively in PowerShell?
- Get File Name Without Extension in PowerShell
- Get Newest File In Directory In PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.