PowerShell: How to Create Folder with Current Date as the Name


You can use the following syntax in PowerShell to create a folder with the current date as the folder name:

New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"

This particular example creates a new folder in the current directory with a name that displays the current date in a yyyy-MM-dd format.

Note: The Get-Date cmdlet in PowerShell returns the current date and time. By using the ToString() method we are able to format this date and time in a specific manner.

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

Example: How to Create Folder with Current Date as Name in PowerShell

Suppose that our current directory has the following path:

  • c:/users/bobbi/data1

We can use the following syntax to list all folders that exist in this directory:

Get-ChildItem -Directory

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

We can see that three folders exist in this directory.

Suppose that we would like to create a new folder whose name is the current date (this article is being written on July 3, 2024) in a yyyy-MM-dd format.

We can use the following syntax to do so:

New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"

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

PowerShell create folder with current date as name

Notice that when we use the Get-ChildItem cmdlet again to view the updated contents of the current directory we can see that a new folder named 2024-07-03 has been created.

Note #1: We used a yyyy-MM-dd format within the ToString() method but feel free to specify a different date format if you would like.

Note #2: You can find the complete documentation for the New-Item cmdlet in PowerShell here.

Related Tutorials

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

PowerShell: How to Find Empty Folders
PowerShell: How to Create Folder if it Does Not Exist
PowerShell: How to Get Most Recently Created Folder from Path

Leave a Reply