How to Get the First 10 Files in a Folder Using PowerShell?

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 10

In this example:

  • Get-ChildItem retrieves all files and directories in the specified folder.
  • Sort-Object sorts the items by the LastWriteTime property in descending order.
  • Select-Object selects 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.

Get the First 10 Files in a Folder Using PowerShell

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 10

In this example:

  • Where-Object filters 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.

How to Get the First 10 Files in a Folder Using PowerShell

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 10

In this example:

  • The -Recurse parameter makes Get-ChildItem search 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-Object processes 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:

PowerShell Get the First 10 Files in a Folder

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.