PowerShell: How to Get Most Recently Created Folder from Path


You can use the following syntax in PowerShell to get the most recently created folder from a particular path:

Get-ChildItem -Directory |
Sort CreationTime -Desc |
Select Name, CreationTime -First 1

This particular example will return the name and creation date of the most recently created folder in the current directory.

Here is what the various commands do:

  • Get-ChildItem -Directory: Retrieves all folders from current directory
  • Sort CreationTime -Desc: Sorts folders based on creation time in descending order
  • Select Name, CreationTime -First 1: Returns only the folder name and creation datetime of the first folder (i.e. the folder with the most recent creation datetime)

The following example shows how to use this syntax in practice.

Example: How to Get Most Recently Created Folder in PowerShell

Suppose that we would like to get the most recently created folder in our current directory.

We can first use the following syntax to view all folder names and their creation dates:

Get-ChildItem -Directory | Select Name, CreationTime

The following screenshot shows how to use this syntax in practice:

We can see that our current directory has four total folders.

We can use the following syntax to retrieve only the most recently created folder:

Get-ChildItem -Directory |
Sort CreationTime -Desc |
Select Name, CreationTime -First 1

The following screenshot shows how to use this syntax in practice:

PowerShell get most recently created folder

We can see that this returns only the folder named owners, which represents the folder created most recently in our current directory.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Get-ChildItem with Filter
PowerShell: How to Use Get-ChildItem to Get Full Path of Files
PowerShell: How to List Files in Directory by Date

Leave a Reply